[英]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];
It gives the error:它给出了错误:
Attempt to mutate immutable object with replaceOccurrencesOfString:withString:options:range:'
尝试用 replaceOccurrencesOfString:withString:options:range:' 改变不可变的 object
And spaces are not replaced with '+'并且空格不替换为“+”
My Initial answer was incorrect , you should (as per Jasarien's answer) use stringByReplacingOccurrencesOfString:withString:options:range
.我的初始回答不正确,您应该(根据 Jasarien 的回答)使用
stringByReplacingOccurrencesOfString:withString:options:range
。 rather than replaceOccurrencesOfString:withString:options:range
而不是
replaceOccurrencesOfString:withString:options:range
NSString
is immutable. NSString
是不可变的。 replaceOccurrencesOfString:withString:options:range:
is only available on NSMutableString
. replaceOccurrencesOfString:withString:options:range:
仅在NSMutableString
上可用。
The method you should be using is:您应该使用的方法是:
stringByReplacingOccurrencesOfString:withString:options:range:
This is implemented on immutable strings, ie NSString
and will return a new string with the replacements you specify.这是在不可变字符串上实现的,即
NSString
并将返回一个带有您指定的替换的新字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.