How to find whether a particular string has unicode characters (esp. Double Byte characters)

后端 未结 6 888
我在风中等你
我在风中等你 2020-12-08 13:55

To be more precise, I need to know whether (and if possible, how) I can find whether a given string has double byte characters or not. Basically, I need to open a pop-up to

6条回答
  •  感动是毒
    2020-12-08 14:38

    JavaScript holds text internally as UCS-2, which can encode a fairly extensive subset of Unicode.

    But that's not really germane to your question. One solution might be to loop through the string and examine the character codes at each position:

    function isDoubleByte(str) {
        for (var i = 0, n = str.length; i < n; i++) {
            if (str.charCodeAt( i ) > 255) { return true; }
        }
        return false;
    }
    

    This might not be as fast as you would like.

提交回复
热议问题