【问题标题】:Xcode - Replace content in '< >' of StringXcode - 替换字符串的“< >”中的内容
【发布时间】:2013-05-20 09:32:45
【问题描述】:

我搜索了这个问题,但没有找到任何有用的东西。

例如,我有这个字符串:

NSString *text = @"Siguiendo la estela del <a class="colorbox" href="http://www.applesfera.com/apple/apple-presenta-un-nuevo-anuncio-del-iphone-y-vuelve-por-su-derroteros">anuncio Photos Every Day en el que la compañía de la manzana nos mostraba situaciones cotidianas para hacer hincapié en que cada día se hacen más fotos con el iPhone que con cualquier otra cámara"

我想消除''标签中的所有内容,(''包括在内),所以我想要的结果是:

NSString *text = @"Siguiendo la estela del anuncio Photos Every Day en el que la compañía de la manzana nos mostraba situaciones cotidianas para hacer hincapié en que cada día se hacen más fotos con el iPhone que con cualquier otra cámara"

我看到使用正则表达式是可能的,但我不知道怎么做。 需要有关我可以在这里做什么的指导。

【问题讨论】:

标签: ios string replace tags expression


【解决方案1】:

对于字符串的简单替换,您可以像这样使用stringByReplacingOccurrencesOfString:withString:options:range:

NSString* result = [text stringByReplacingOccurrencesOfString: @"<.*?>"
                                                   withString: @""
                                                      options: NSRegularExpressionSearch
                                                        range: NSMakeRange(0, text.length)];

【讨论】:

    【解决方案2】:

    看看这个链接https://stackoverflow.com/a/4886998。我想这就是你要找的。如果您想在许多地方恢复它,请按照该帖子中的说明执行。 否则就这样使用

    NSRange r;
    NSString *s = @"my<remove>output";
    while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        s = [s stringByReplacingCharactersInRange:r withString:@""];
    

    【讨论】:

      猜你喜欢
      • 2011-05-06
      • 2019-10-31
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 2011-10-24
      相关资源
      最近更新 更多