gpt4 book ai didi

iphone - Objective C 中的多个字符串替换

转载 作者:行者123 更新时间:2023-12-03 20:22:27 24 4
gpt4 key购买 nike

我知道这是错误的方法(因为它会出错),但你可以看到我正在尝试做什么:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com