【问题标题】:Remove link in NSString删除 NSString 中的链接
【发布时间】:2012-03-26 11:31:42
【问题描述】:

我想删除我的 NSString 的一部分。 我有这个:@"Test Hello http://www.google.fr wolrd" 我想输出:@"Test Hello other world"

一些想法? 谢谢 :)

【问题讨论】:

  • [yourString stringByReplacingOccurancesOfString:@"http://google.fr" withString:@"other"];
  • 别忘了[yourString stringByReplacingOccurancesOfString:@"wolrd" withString:@"world"]; ;-)
  • 我的字符串可以是google.fr,但也可以是facebook.frtwitter.fr 之类的其他字符串...
  • 字符串总是host.tld吗?删除任意的“whatever”字符串会很困难。
  • 我只需要删除字符串中的 url。

标签: iphone xcode string nsstring


【解决方案1】:

您可以使用NSDataDetector 在字符串中搜索任何链接(当然是按照 Apple 的公式)。

NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];

然后你可以使用stringByReplacingMatchesInString:options:range:withTemplate: 将所有链接替换为空字符串。

【讨论】:

    【解决方案2】:

    您可以使用 RegexKitLite 之类的正则表达式库,使用 stringByReplacingOccurrencesOfRegex 等方法从字符串中过滤任何 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多