Recognize special characters in NSString

我与影子孤独终老i 提交于 2020-01-02 20:03:08

问题


I have a UITextView with text:

🅰🅱🍉👌📧🎏❡

its image:

(This is screen shot of my text, I worry somebody can not see my special character)

If I am an end-user, I just see that there are only 7 characters on textView.

But [textView.text length]=13

I want to split this text into an array like this:

array = @[

,

,

,... ]

But I can't detect where

is, where

is, where

is ... :(

Could you help me!


回答1:


Characters outside of the "basic multilingual plane" - in other words, characters whose Unicode value is greater than U+FFFF - are stored in NSString as two UTF-16 characters, a so-called surrogate pair.

For example, "👌" = U+1F44C (OK HAND SIGN) is stored in NSString as the two characters U+D83D, U+DC4C.

To correctly split a string with surrogate pairs, use the

enumerateSubstringsInRange:options:usingBlock:

method of NSString with the NSStringEnumerationByComposedCharacterSequences option.

Another useful method is rangeOfComposedCharacterSequenceAtIndex: to determine the range of a composed character sequence at a certain position.



来源:https://stackoverflow.com/questions/22499381/recognize-special-characters-in-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!