javascriptunicodedouble-byte

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


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 display a given text which can contain double byte characters, like Chinese or Japanese. In this case, we need to adjust the window size than it would be for English or ASCII. Anyone has a clue?


Solution

  • 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.