【问题标题】:How to omit a certain substring out of an NSString?如何从 NSString 中省略某个子字符串?
【发布时间】:2012-04-28 17:46:35
【问题描述】:

我想知道如何在 NSString 中省略特定的子字符串,假设 NSString 确实包含该子字符串。

例如:

原始字符串:“这是一个字符串但这些单词应该省略。”

要省略的子字符串:“但这些词应该省略”。

结果字符串:“这是一个字符串。”

谢谢你, 抒情的。

【问题讨论】:

    标签: objective-c ios xcode nsstring


    【解决方案1】:
    NSString *originalString = @"This is a string but these words should be omitted.";
    NSString *substringToOmit = @" but these words should be omitted";
    NSString *resultString = [originalString stringByReplacingOccurencesOfString:substringToOmit
                                                                      withString:@""];
    

    【讨论】:

      【解决方案2】:

      参见NSStringstringByReplacingOccurrencesOfString:withString:。您可能还想用stringByTrimmingCharactersInSet: 修剪尾随空格。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-02
        相关资源
        最近更新 更多