NSString字元編碼

2021-06-05 22:40:51 字數 4845 閱讀 5857

最近再開發ios程式的時候,發現從url取得的utf8編碼資料中文部分顯示為亂碼,為此在網上查詢了一些資料,對nsstring的編碼問題做一下總結:

幫助文件列出的nsstring支援的編碼:

enum ;

列表中很多編碼都沒有,原來這個列表沒有把全部的編碼列出來,通過以下**可以列出nsstring支援的全部編碼:

執行結果:

western (mac os roman) = 30

japanese (mac os) = -2147483647

traditional chinese (mac os) = -2147483646

korean (mac os) = -2147483645

arabic (mac os) = -2147483644

hebrew (mac os) = -2147483643

greek (mac os) = -2147483642

cyrillic (mac os) = -2147483641

devanagari (mac os) = -2147483639

gurmukhi (mac os) = -2147483638

gujarati (mac os) = -2147483637

thai (mac os) = -2147483627

simplified chinese (mac os) = -2147483623

tibetan (mac os) = -2147483622

central european (mac os) = -2147483619

symbol (mac os) = 6

dingbats (mac os) = -2147483614

turkish (mac os) = -2147483613

croatian (mac os) = -2147483612

icelandic (mac os) = -2147483611

romanian (mac os) = -2147483610

celtic (mac os) = -2147483609

gaelic (mac os) = -2147483608

keyboard symbols (mac os) = -2147483607

farsi (mac os) = -2147483508

cyrillic (mac os ukrainian) = -2147483496

inuit (mac os) = -2147483412

unicode (utf-16) = 10

unicode (utf-7) = -2080374528

unicode (utf-8) = 4

unicode (utf-32) = -1946156800

unicode (utf-16be) = -1879047936

unicode (utf-16le) = -1811939072

unicode (utf-32be) = -1744830208

unicode (utf-32le) = -1677721344

western (iso latin 1) = 5

central european (iso latin 2) = 9

western (iso latin 3) = -2147483133

central european (iso latin 4) = -2147483132

cyrillic (iso 8859-5) = -2147483131

arabic (iso 8859-6) = -2147483130

greek (iso 8859-7) = -2147483129

hebrew (iso 8859-8) = -2147483128

turkish (iso latin 5) = -2147483127

nordic (iso latin 6) = -2147483126

thai (iso 8859-11) = -2147483125

baltic (iso latin 7) = -2147483123

celtic (iso latin 8) = -2147483122

western (iso latin 9) = -2147483121

romanian (iso latin 10) = -2147483120

latin-us (dos) = -2147482624

greek (dos) = -2147482619

baltic (dos) = -2147482618

western (dos latin 1) = -2147482608

greek (dos greek 1) = -2147482607

central european (dos latin 2) = -2147482606

cyrillic (dos) = -2147482605

turkish (dos) = -2147482604

portuguese (dos) = -2147482603

icelandic (dos) = -2147482602

hebrew (dos) = -2147482601

canadian french (dos) = -2147482600

arabic (dos) = -2147482599

nordic (dos) = -2147482598

russian (dos) = -2147482597

greek (dos greek 2) = -2147482596

thai (windows, dos) = -2147482595

japanese (windows, dos) = 8

simplified chinese (windows, dos) = -2147482591

korean (windows, dos) = -2147482590

traditional chinese (windows, dos) = -2147482589

western (windows latin 1) = 12

central european (windows latin 2) = 15

cyrillic (windows) = 11

greek (windows) = 13

turkish (windows latin 5) = 14

hebrew (windows) = -2147482363

arabic (windows) = -2147482362

baltic (windows) = -2147482361

vietnamese (windows) = -2147482360

western (ascii) = 1

japanese (shift jis x0213) = -2147482072

chinese (gbk) = -2147482063

chinese (gb 18030) = -2147482062

japanese (iso 2022-jp) = 21

japanese (iso 2022-jp-2) = -2147481567

japanese (iso 2022-jp-1) = -2147481566

chinese (iso 2022-cn) = -2147481552

korean (iso 2022-kr) = -2147481536

japanese (euc) = 3

simplified chinese (gb 2312) = -2147481296

traditional chinese (euc) = -2147481295

korean (euc) = -2147481280

japanese (shift jis) = -2147481087

cyrillic (koi8-r) = -2147481086

traditional chinese (big 5) = -2147481085

western (mac mail) = -2147481084

simplified chinese (hz gb 2312) = -2147481083

traditional chinese (big 5 hkscs) = -2147481082

ukrainian (koi8-u) = -2147481080

traditional chinese (big 5-e) = -2147481079

western (nextstep) = 2

non-lossy ascii = 7

western (ebcdic latin core) = -2147480575

western (ebcdic latin 1) = -2147480574

從結果中可以看到:

unicode (utf-8) = 4        utf8編碼4

所以,從url取得utf編碼的資料時,可以這樣處理:

最後列印出的中文顯示正常了。

NSString 去除特殊字元

字串過濾 nscharacterset donotwant nscharacterset charactersetwithcharactersinstring 先將 tempstring 用上面的 set 截成乙個陣列,然後在用這個陣列拼成乙個字串 tempstring tempstring com...

NSString字元高亮演算法

在開發過程中,遇到的一些演算法問題,寫在這裡記錄一下 搜尋匹配演算法 高亮中間的字串 nsstring astring i am a string,not a astring,just a string nsmutableattributedstring newsetsearchresultstri...

NSString字串擷取

1.擷取字串一部分 substringwithrange nsrange nsstring a 01234567890 nsstring b a substringwithrange nsmakerange 0,2 b 01 2.字串擷取到第n位 substringtoindex n 第n位不在擷取...