【问题标题】:Delete text after #example iOS#example iOS 之后删除文本
【发布时间】:2015-03-03 19:19:46
【问题描述】:

如何在一个单词之后删除 NSStringNSArray 的一部分,例如 #gallery-1 或两个“跳行”之后“?而且每次出现的句子都可以删掉吗?

我想删除 #gallery-1 之后或此 NSString(或 NSArray)中的 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.

【问题讨论】:

  • 您查看过NSString 文档吗?有一种方法substringToIndex: 可以与rangeOfString: 结合使用。
  • @IanMacDonald 我会检查的,谢谢!

标签: ios objective-c nsstring nsarray


【解决方案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:@""];

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

拆分

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 中,我们真的只需要第一个。

【讨论】:

  • 只删除#gallery-1,但不是#gallery-1 后面的所有文本:/但这将帮助我删除“点击此处查看实时峰值网络摄像头”
  • 您可以将任何内容放入该替换字符串 - @#gallery-1 "。因此,如果您需要删除空格...将空格放在那里.. @""
  • 如果您需要删除其他文本..只需使用 @"WEBCAM".... 或其他行...当然整个工作就是几行..
  • 是的,但是看看我在#gallery-1 之后的数据...有很多!
  • 什么是很多?一百字……还是一百万字?不用担心系统可以在几毫秒内处理。
【解决方案2】:

嗯,离我的头顶有点远,输入是上面的字符串。

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

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

要摆脱“点击此处查看实时峰值网络摄像头”:

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

【讨论】:

  • 哦,非常感谢您的回复,但我在 Objective-C 工作:(
猜你喜欢
  • 1970-01-01
  • 2021-02-23
  • 2013-04-10
  • 1970-01-01
  • 2015-02-16
  • 2014-05-30
  • 1970-01-01
  • 2016-08-24
  • 2020-11-03
相关资源
最近更新 更多