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!"
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:
Another option for regex is RegexKitLite, which is very easy to embed in an iPhone project:
OS X 10.7+ and iOS 3.2+
Use the native regexp solution provided by hfossli.
Otherwise
Either 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
final
givesPossible alternative regex patterns:
[ ]+
[ \\t]+
\\s+
Performance rundown
Ease of extension, performance, number lines of code and the number of objects created makes this solution appropriate.
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
NSScanner
would probably work best but that'd only be necessary if you plan to process huge (many megabytes) blocks of text.Actually, there's a very simple solution to that:
(Source)