删除#example iOS 之后的文字

Delete text after #example iOS

如何在单词后删除 NSStringNSArray 的一部分,例如 #gallery-1 还是两个 "skip lines" 之后?以及每次出现的句子是否可以删除?

我想擦除此 NSString(或 NSArray)中 #gallery-1 之后或 2 个跳过行之后的所有文本并擦除 "CLICK HERE FOR 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.

嗯嗯,我想出了一些方法,其中输入是上面的字符串。

如您所说,这会将字符串的组成部分分开 "skipped lines"。

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

去掉"CLICK HERE FOR LIVE PEAK WEBCAM":

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

非常简单。只需使用 NSString class.

中的实例方法

移除事件

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:@""];

显然,原始字符串可以包含更长的多次出现的字符串。因此,如果它包含更多它们,所有出现的地方都将以单行方法为您替换。真的再简单不过了。

拆分

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];

这里的字符串被分成了很多部分(每一个部分都是一个字符串,基本上是把文本切成小块),"delimiter" 是任何作为参数的东西。所有组件都在 NSArray 中,我们只需要第一个。