您的位置:首页 > 移动开发 > IOS开发

xcod7开发下 替换字符串中的某个元素

2015-10-15 18:41 531 查看
之前看到一些 感觉不是很清晰,自己写了一点,记录下来方便以后用。。。
NSString *testStr = @"qwqweqweqew\n啊是肯德基阿里的骄傲是假的大 阿什顿\n睡觉奥萨杰克丹尼 撒1231\n刘";

NSMutableString *resultStr = [[NSMutableString alloc] initWithString:testStr];

[resultStr replaceOccurrencesOfString:@"\n" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [testStr length])];

NSLog(@"%@", resultStr);

输出结果:



补充:

typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {
NSCaseInsensitiveSearch = 1, //不区分大小写比较
NSLiteralSearch = 2, //区分大小写比较
NSBackwardsSearch = 4, //从字符串末尾开始搜索
NSAnchoredSearch = 8, //搜索限制范围的字符串
NSNumericSearch = 64, //按照字符串里的数字为依据,算出顺序。例如 Foo2.txt < Foo7.txt < Foo25.txt
NSDiacriticInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 128, //忽略 "-" 符号的比较
NSWidthInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 256, //忽略字符串的长度,比较出结果
NSForcedOrderingSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 512, //忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending

NSRegularExpressionSearch NS_ENUM_AVAILABLE(10_7, 3_2) = 1024 //只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS xcode nsstring