如何查找特定字符串是否具有unicode字符(尤其是双字节字符) [英] How to find whether a particular string has unicode characters (esp. Double Byte characters)

查看:173
本文介绍了如何查找特定字符串是否具有unicode字符(尤其是双字节字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更确切地说,我需要知道是否(以及如果可能,如何)我可以找到给定字符串是否具有双字节字符。基本上,我需要打开一个弹出窗口来显示一个给定的文本,它可以包含双字节字符,如中文或日文。在这种情况下,我们需要调整窗口大小,而不是英文或ASCII。
任何人都有线索?

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?

推荐答案

JavaScript将文本内部保存为UCS-2,它可以编码相当广泛的子集Unicode。

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.

这篇关于如何查找特定字符串是否具有unicode字符(尤其是双字节字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆