繁体   English   中英

从单词之间删除空格并替换为“+”

[英]remove space from in between words and replace with '+'

NSString *locations=[[NSString alloc] init];
    locations=[mainView getLocationText];
    NSMutableArray *cs=[NSMutableArray arrayWithArray:[locations componentsSeparatedByString:@","]];
    NSString *city=[[cs objectAtIndex:0] replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[[cs objectAtIndex:0] length])];
    NSString *state=[[cs objectAtIndex:1] replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[[cs objectAtIndex:0] length])];
    NSMutableString *otherParams = [[[NSMutableString alloc] initWithFormat:@"city=%@&state=%@&beds=%@&baths=%@&minprice=%@&maxprice=%@&searchType=%@&page=1",city,state,bedsVal,bathsVal,minPriceVal,maxPriceVal,propTypeVal] autorelease];    

它给出了错误:

尝试用 replaceOccurrencesOfString:withString:options:range:' 改变不可变的 object

并且空格不替换为“+”

我的初始回答不正确,您应该(根据 Jasarien 的回答)使用stringByReplacingOccurrencesOfString:withString:options:range 而不是replaceOccurrencesOfString:withString:options:range

NSString是不可变的。 replaceOccurrencesOfString:withString:options:range:仅在NSMutableString上可用。

您应该使用的方法是:

stringByReplacingOccurrencesOfString:withString:options:range:

这是在不可变字符串上实现的,即NSString并将返回一个带有您指定的替换的新字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM