2013-02-20 128 views
0

我有一個字符串,我想刪除字符之間的字符和添加字符我怎麼能這樣做?如何刪除字符串中兩個字符之間的字符?

NSString *strUrl [email protected]"str&mn=12&yr=2012str"; 
int month =10, int year =2013; 

我想從strul刪除字符12和& MN = &年

+1

您即將在過低的水平的問題;而是編寫一些將這些參數解析爲字典的東西,以及將從參數字典中重新創建URL的內容,包括執行URL編碼。 – trojanfoe 2013-02-20 13:08:59

+0

請參閱 http://stackoverflow.com/questions/2119640/best-way-to-split-and-convert-url-params-into-string-values – uchuugaka 2013-02-20 13:44:10

回答

0

看起來你只是想 '10' 來代替 '12' 之間追加10同一年。

使用stringByReplacingOccurrencesOfString:

NSString *strUrl = [@"str&mn=12&yr=2012str" stringByReplacingOccurrencesOfString:@"12" withString:@"10"]; 
+0

12是解析器饋送值,因此它不是常量 – Ravindhiran 2013-02-20 13:09:14

-1

我建議你將使用拆分:

var fields = *strUrl.split("&"); //splits the *strurl by "&" 
var newString = fields[0]+"&"+fields[1].split("=")[0]+"="+month+fields[2].split("=")[0]+"="+year+"str"; //builds the new assembled string 
+0

12是解析器提要值,因此它不是常量 – Ravindhiran 2013-02-20 13:08:50

+0

此解決方案不會將其視爲常量,而是使用它在您發佈的字符串結構中的相對位置。 – Wasafa1 2013-02-20 13:10:07

相關問題