![](/img/trans.png)
[英]How to remove space/blank between any two words using RegexKitLite?
[英]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.