How do I strip out the white space chars from an NSString ?
Consider the following example:
NSString *tmpString = @" Hello this is a long string! ";
I want to convert that to: "Hello this is a long string!". I have tried a function like this, but it's not working:
-(NSString *)cleanupString:(NSString *)theString
{
// Remove extra whitespace
NSString *theStringTrimmed = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Split it on whitespace
NSArray *wordsInStringArray = [theStringTrimmed componentsSeparatedByString:@" "];
// Now join it together again
NSString *returnString = [[NSString alloc] init];
for ( NSString* thisElement in wordsInStringArray )
{
returnString = [ returnString stringByAppendingString:[NSString stringWithFormat:@"%@_",thisElement] ];
}
return returnString;
}