Ios 目标C——如何用'+';

Ios 目标C——如何用'+';,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,当句子中有空格时,如何添加加号(+) 例如: Hello how are you doing 我想要的是: Hello+how+are+you+doing 尝试: 尝试NSString类中的stringByReplacingOccurrencesOfString:withString:方法您可以执行如下操作: NSString *helloWorld = @"Hello How are you doing"; NSString *alteredString = [helloWorld str

当句子中有空格时,如何添加加号(+)

例如:

Hello how are you doing
我想要的是:

Hello+how+are+you+doing
尝试:


尝试NSString类中的
stringByReplacingOccurrencesOfString:withString:
方法

您可以执行如下操作:

NSString *helloWorld = @"Hello How are you doing";
NSString *alteredString = [helloWorld stringByReplacingOccurrencesOfString:@" " withString:@"+"];
stringByReplacingOccurrencesOfString:@“with string:@“+”
就是您在这里看到的内容


祝你好运。

+1很好的超快速回答不是它错了,而是为什么要使用NSMutbaleString?@vikingosegundo他没有修改字符串本身,而是创建了一个新的、经过修改的实例。为此,不需要可变字符串。@H2CO3,哦,我应该回去睡觉了。@vikingosegundo或者OP需要修复他的代码;)
 NSMutableString *nstring = [NSMutableString stringWithFormat:@"Hello how are you doing"];

   NSString *newString =  [nstring stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSLog(@"%@",newString);
 NSMutableString *nstring = [NSMutableString stringWithFormat:@"Hello how are you doing"];

   NSString *newString =  [nstring stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSLog(@"%@",newString);