NSString 取代特定字元(字串)

  在處理字元的時候,總是會遇到一些無法正確顯示的字元。
 一種變通的方式就是用"取代"的方式來轉換容易出問題的字串,譬如在url上傳輸#@!這些字元容易造成轉換錯誤,可以透過該method來轉換成utf code。
 像# 就可以轉換成%23,這樣送給url處理時會自動轉譯成#, 想要查詢URL編碼 可以來到這邊 來查詢(記得要選encode)。
NSString *urlString =
    [NSString stringWithFormat:@"%@/iphone/push/create?pn[token]=%@&pn[send_at]=%@",
     kHTTPURL, appDelegate.deviceAPNToken, [dateTimeToUse description]];

urlString =
    [[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
     stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
See also :

  1. http://stackoverflow.com/questions/898488/make-an-nsurl-with-an-encoded-plus-2b

留言

這個網誌中的熱門文章

Office 2021 離線安裝封裝與KMS啟動步驟

Ollama使用心得與模型導入教學

ARC下NSMutableDictionary 無法使用retainCount