如何在NSMutableString中修剪“”和“\ n”?
答案 0 :(得分:12)
NSCharacterSet* charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@" \n"];
NSString* trimmedStr = [aStr stringByTrimmingCharactersInSet:charsToTrim];
答案 1 :(得分: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:@""];
答案 2 :(得分:0)
如何调用replaceOccurrencesOfString:withString:options:range两次,替换“”然后“\ n”替换为什么?
答案 3 :(得分:0)
NSString *trimmed = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];