Is there any function to remove the white spaces from text message in objective c?
For eg:for "How are you",the r开发者_StackOverflowesult should be "howareyou"
Thanks in advance.
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
Tom's approach isn't very efficient. What you want is:
-stringByTrimmingCharactersInSet:
NSString *myString = @"How are you";
myString = [myString stringByReplacingOccurrencesOfString:@" " withString:@""];
精彩评论