發現nsstring類中有內建的方法可以實現。他們分別是:
- (nsstring *)stringbyaddingpercentescapesusingencoding:(nsstringencoding)encoding
- (nsstring *)stringbyreplacingpercentescapesusingencoding:(nsstringencoding)encoding
只要傳入相應的編碼即可以進行編碼和解碼了,不過此方法是對整個url進行編碼的所以如果有query string中帶有&?/等是不會進行編碼轉換的,如果有這方面的需要的朋友可以參照下面提供的**,找到解決方法:
示例:[cpp]view plain
copy
nsstring* string1 = @
"資料夾"
; nsstring* string2 = [string1 stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
nsstring* string3 = [string2 stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
nsstring* string4 = [string2 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
nsstring* string5 = [string3 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
nsstring* string6 = [string4 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
nsstring* string7 = [string5 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
輸出結果: string5 & 6 重複編碼encode
string1:資料夾
string2:%e6%96%87%e4%bb%b6%e5%a4%b9
string3:%25e6%2596%2587%25e4%25bb%25b6%25e5%25a4%25b9
string4:資料夾
string5:%e6%96%87%e4%bb%b6%e5%a4%b9
string6:資料夾
string7:資料夾
Objective C學習 中文URL編碼和解碼
發現nsstring類中有內建的方法可以實現。他們分別是 nsstring stringbyaddingpercentescapesusingencoding nsstringencoding encoding nsstring stringbyreplacingpercentescapesusin...
Objective C學習 中文URL編碼和解碼
發現nsstring類中有內建的方法可以實現。他們分別是 nsstring stringbyaddingpercentescapesusingencoding nsstringencoding encoding nsstring stringbyreplacingpercentescapesusin...
Objective C學習 中文URL編碼和解碼
發現nsstring類中有內建的方法可以實現。他們分別是 nsstring stringbyaddingpercentescapesusingencoding nsstringencoding encoding nsstring stringbyreplacingpercentescapesusin...