开发者

Truncate white space in text in iphone/objective c

开发者 https://www.devze.com 2022-12-08 14:14 出处:网络
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\"

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

精彩评论

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