Consider the following example:
" Hello this is a long string! "
I want to convert that to:
"Hello this is a long string!"
Consider the following example:
" Hello this is a long string! "
I want to convert that to:
"Hello this is a long string!"
With a regex, but without the need for any external framework:
according from @Mathieu Godart is best answer, but some line is missing , all answers just reduce space between words , but when if have tabs or have tab in place space , like this: " this is text \t , and\tTab between , so on " in three line code we will : the string we want reduce white spaces
the result is
without replacing tab the resul will be:
Following two regular expressions would work depending on the requirements
Then apply nsstring's instance method
stringByReplacingOccurrencesOfString:withString:options:range:
to replace them with a single white space.e.g.
Note: I did not use 'RegexKitLite' library for the above functionality for iOS 5.x and above.
A one line solution:
This should do it...
Here's a snippet from an
NSString
extension, where"self"
is theNSString
instance. It can be used to collapse contiguous whitespace into a single space by passing in[NSCharacterSet whitespaceAndNewlineCharacterSet]
and' '
to the two arguments.