2012-10-31 90 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:@" " 

享受編碼。