2012-10-31 45 views
-2

可能重复:
Replacing one character in a string in Objective-C
Remove all whitespace from NSString如何从一个字符串中的iOS删除空格

@"this string contains blank spaces"

我们该如何将此字符串转换为@"thisstringcontainsblankspaces"

我的意思是我们如何修剪一个字符串中的所有空格和换行符。我已经通过使用whitespaceCharacterSet尝试过NSCharecterSet。但是这只能删除字符串末端的空格,而不能在两个字符之间......

任何想法?

回答

5

使用此代码删除空格:

NSString *str = @"this string contains blank spaces"; 
NSString *newStr = [str stringByReplacingOccurrencesOfString:@" " withString:@""]; 
3
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""]; 
0

使用下面的代码:

[YourString stringByReplacingOccurrencesOfString:@" " withString:@""]; 

你可以通过它你想在

替换任何字符或单词
stringByReplacingOccurrencesOfString:@" " 

并将字/字符串/字符:

withString:@" " 

享受编码。