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:
I want to convert that to:
|
||||
|
|
OS X 10.7+ and iOS 3.2+Use the native regexp solution provided by hfossli. OtherwiseEither use your favorite regexp library or use the following Cocoa-native solution:
|
|||||||||||||||||||||
|
|
Regex and NSCharacterSet is here to help you. This solution trims leading and trailing whitespace as well as multiple whitespaces.
Logging
Possible alternative regex patterns:
Ease of extension, performance, number lines of code and the number of objects created makes this solution appropriate. |
|||||||||||||||||
|
|
Actually, there's a very simple solution to that:
(Source) |
|||||||||||||||||||||
|
|
With a regex, but without the need for any external framework:
|
|||||||||
|
|
A one line solution:
|
|||||||||||||||||
|
|
This should do it...
|
|||||||||||||||||
|
|
Another option for regex is RegexKitLite, which is very easy to embed in an iPhone project:
|
||||
|
|
|
Try This
|
|||
|
|
|
Here's a snippet from an
|
||||
|
|
|
Alternative solution: get yourself a copy of OgreKit (the Cocoa regular expressions library). The whole function is then:
Short and sweet. If you're after the fastest solution, a carefully constructed series of instructions using |
|||||||||
|
|
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:
|
|||
|
|
|
You can also use a simple while argument. There is no RegEx magic in there, so maybe it is easier to understand and alter in the future:
|
|||||
|
|
Following two regular expressions would work depending on the requirements
Then apply nsstring's instance method e.g.
Note: I did not use 'RegexKitLite' library for the above functionality for iOS 5.x and above. |
|||||||||
|