如何在句子中有空格時添加加號(+)?Objective-C - 如何用'+'替換空格
例子:
Hello how are you doing
我要的是:
Hello+how+are+you+doing
如何在句子中有空格時添加加號(+)?Objective-C - 如何用'+'替換空格
例子:
Hello how are you doing
我要的是:
Hello+how+are+you+doing
嘗試:
NSString *str = @"Hello how are you doing";
str = [str stringByReplacingOccurrencesOfString:@" " withString:@"+"];
嘗試從NSString類stringByReplacingOccurrencesOfString:withString:
方法
你可以做什麼像這樣:
NSString *helloWorld = @"Hello How are you doing";
NSString *alteredString = [helloWorld stringByReplacingOccurrencesOfString:@" " withString:@"+"];
stringByReplacingOccurrencesOfString:@" " withString:@"+"
特別是你在這裏看到的。
祝你好運。
NSMutableString *nstring = [NSMutableString stringWithFormat:@"Hello how are you doing"];
NSString *newString = [nstring stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSLog(@"%@",newString);
這並不是說這是錯的,但爲什麼一個NSMutbaleString? – Josiah 2013-02-13 14:00:24
@vikingosegundo然而,他並沒有修改字符串本身,而是創建了一個新的,已更改的實例。爲此,不需要可變字符串。 – 2013-02-13 14:07:51
@ H2CO3,哦,我應該回去睡覺。 – vikingosegundo 2013-02-13 14:08:25
+1不錯超快的答案 – 2013-02-13 13:52:30