Replace consecutive space, tab, newline

I have an NSString followed by a space, tab, newlines. Is there a way to reproduce them with just one space? I have a little UIlabel view to display them.

+3
source share
1 answer

You can try:

NSString *string = @" spaces in front and at the end ";
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                              [NSCharacterSet whitespaceAndNewlineCharacterSet]];

The key here is whitespaceAndNewlineCharacterSet, from Apple doc :

Returns a character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
+4
source

All Articles