我知道这是错误的方法(因为它会出错),但你可以看到我正在尝试做什么:
NSString *urlTerm = [terms stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSString *urlTerm = [terms stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *urlTerm = [terms stringByReplacingOccurrencesOfString:@"&" withString:@""];
NSString *urlTerm = [terms stringByReplacingOccurrencesOfString:@"'" withString:@""];
NSString *urlTerm = [terms stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSString *urlTerm = [terms stringByReplacingOccurrencesOfString:@"_" withString:@""];
我有一个名为“term”的搜索词,想要将清理后的版本输出到“urlTerm”中?
最佳答案
-stringByReplacingOccurrencesOfString:
返回一个新字符串。原始字符串terms
不会受到此函数的影响。 即使 (好吧,原来“双空格”是一个制表符。:| )terms
会受到影响,第二条语句也永远不会成功,因为所有空格都已经在第一条语句中变成了+
,并且没有留下双空格。
你可以使用
NSString* urlTerm = terms;
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@"\t" withString:@""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@"&" withString:@""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@"'" withString:@""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@"-" withString:@""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@"_" withString:@""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@" " withString:@"+"];
关于iphone - Objective C 中的多个字符串替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4182545/