如何修剪 NSMutableString 中的“”和“\n”?
问问题
5676 次
4 回答
12
NSCharacterSet* charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@" \n"];
NSString* trimmedStr = [aStr stringByTrimmingCharactersInSet:charsToTrim];
于 2009-11-21T07:25:55.510 回答
1
我知道这是一个老问题,但是是的,可以修剪可变字符串而无需返回新字符串。你只需要一个正则表达式,下面的例子:
NSMutableString *mutable = [NSMutableString stringWithString:@" String to trim \n"];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\n| | $|^ " options:0 error:nil];
[regex replaceMatchesInString:mutable options:0 range:NSMakeRange(0, [mutable length]) withTemplate:@""];
于 2013-07-10T20:27:09.123 回答
0
如何调用 replaceOccurrencesOfString:withString:options:range 两次,用什么都替换 " " 然后 "\n"?
于 2009-11-21T07:07:22.223 回答
0
NSString *trimmed = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
于 2011-08-04T17:21:55.770 回答