簡體   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