Objective-c – Truncate white space in text in iphone/objective c

iphoneobjective c

Is there any function to remove the white spaces from text message in objective c?

For eg:for "How are you",the result should be "howareyou"

Thanks in advance.

Best Answer

You could use NSString's componentsSeparatedByCharactersInSet with whitespaceCharacterSet to first split the string on the whitespace, and then join the components using NSArray's componentsJoinedByString.

eg.

NSString *myString=@"How are you";
myString = [[myString componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString: @""];
NSLog(myString); // displays Howareyou