How to trim "and" \ n "in NSMutableString

How to trim "and" \ n "in NSMutableString?

+4
source share
4 answers
NSCharacterSet* charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@" \n"]; NSString* trimmedStr = [aStr stringByTrimmingCharactersInSet:charsToTrim]; 
+12
source

I know this is an old question, but yes, you can trim a mutable string without having to return a new string. You just need a regular expression, an example below:

 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:@""]; 
+1
source

What about calling replaceOccurrencesOfString: withString: options: range twice, replacing "and then" \ n "with nothing?

0
source
 NSString *trimmed = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
0
source

All Articles