2017-02-23 37 views
0

我保存的NSString這個URL字符串替換:在URL字符串檢測特定字符,並用另一個字符

NSString *currentURL = self.webView.request.URL.absoluteString; 

https://www.tripresso.com/cooperation/toyota?utm_source=cooperation&utm_medium=toyota&utm_campaign=toyota_app?dxid=7019b21e-bd3b-4bde-8917-f353149bdda4&appid=com.hotai.toyota.citydriver.official 

正如你可以看到有這個字符串是2 ?。我的問題是我怎樣才能用&替換第二個?

回答

0

如果要刪除所有,但第一次出現,那麼這應該工作:

NSString *currentURL = self.webView.request.URL.absoluteString; 
NSMutableArray *components = [[currentURL componentsSeparatedByString:@"?"] mutableCopy]; 
NSString *firstComponent = [components firstObject]; 
[components removeObject:firstComponent]; 
NSString *newString = [NSString stringWithFormat:@"%@?%@", firstComponent, [components componentsJoinedByString:@"&"]]; 
相關問題