开发者

Truncate white space in text in iphone/objective c

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:@""];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜