在#example iOS之后删除文字

时间:2015-03-03 19:19:46

标签: ios objective-c nsstring nsarray

如何在单词后删除 NSString NSArray 的一部分,例如#gallery-1 或两个&#34之后;跳过行" ?是否可以删除每次出现的句子?

我想在此NSString(或NSArray)中删除#gallery-1之后或2个跳过行之后的所有文本并擦除"点击此处获取LIVE PEAK WEBCAM" :

3rd March
Swell is 5 foot and cross shore, bitter in the cold wind. Maybe streedagh.

High tide: 1703 3.6m     CLICK HERE FOR LIVE PEAK WEBCAM
Low Tide: 1041 0.8m
3 day forecast to March 5th
Good swell all weekend but the winds are changing. Keep in an eye for gaps in the wind.


            #gallery-1 {
                margin: auto;
            }
            #gallery-1 .gallery-item {
                float: left;
                margin-top: 10px;
                text-align: center;
                width: 50%;
            }
            #gallery-1 img {
                border: 2px solid #cfcfcf;
            }
            #gallery-1 .gallery-caption {
                margin-left: 0;
            }
            /* see gallery_shortcode() in wp-includes/media.php */








 
 Wind Charts
         Wind Guru       XC Weather       Buoy Weather
Swell Charts
                                     Magic Seaweed      MSM WAM          Marine Institute
Pressure, Weather, Tides
                      BBC Pressure      Met Eireann      Irish Tide Tables

我想获得:

3rd March
    Swell is 5 foot and cross shore, bitter in the cold wind. Maybe streedagh.

    High tide: 1703 3.6m     CLICK HERE FOR LIVE PEAK WEBCAM
    Low Tide: 1041 0.8m
    3 day forecast to March 5th
    Good swell all weekend but the winds are changing. Keep in an eye for gaps in the wind.

2 个答案:

答案 0 :(得分:1)

非常简单。只需使用NSString类中的实例方法。

取消发生

NSString *originalString = @"#gallery-1 {margin: auto;}#gallery-1 .gallery-item {float: left;margin-top: 10px;text-align: center;width: 50%;}";

NSString *processedString = [originalString stringByReplacingOccurrencesOfString:@"#gallery-1" withString:@""];

显然,原始字符串可以包含多次出现的字符串。因此,如果它包含更多它们,则所有实例都将在单行方法中替换为您。它真的不容易。

SPLIT

NSString *originalString = @"3rd March Swell is 5 foot and cross shore, bitter in the cold wind. Maybe streedagh.     High tide: 1703 3.6m     CLICK HER FOR LIVE PEAK WEBCAM  Low Tide: 1041 0.8m    3 day forecast to March 5th    Good swell all weekend but the winds are changing. Keep in an eye for gaps in the wind.#gallery-1 {margin: auto;}#gallery-1 .gallery-item {float: left;margin-top: 10px;text-align: center;width: 50%;}";

NSString *processedString = [[originalString componentsSeparatedByString:@"#gallery"] firstObject];

这里的字符串被分成许多组件(每个组件都是一个字符串,基本上文本被切成碎片)和"分隔符"是作为参数放置的任何东西。所有组件都在NSArray中,我们真的只需要第一个组件。

答案 1 :(得分:0)

嗯,离我头顶几步,输入就是上面的字符串。

这会将字符串的组成部分分开两个“跳过的行”。

let finalComponents = input.componentsSeperatedByString("\n\n")
let wantedInfo = finalComponents[0] as String

要摆脱“点击此处获取LIVE PEAK WEBCAM”:

let unwanted = "CLICK HERE FOR LIVE PEAK WEBCAM"
let finalString = wantedInfo.stringByReplacingOccurrencesOfString(unwanted, replacement:"")