JS判断中文字符

方法一 http://blog.youkuaiyun.com/qiujiahao/archive/2007/08/09/1733169.aspx 在unicode 字符串中,中文的范围是在4E00..9FFF:CJK Unified Ideographs。 通过对字符的unicode编码进行判断来确定字符是否为中文。 protected bool IsChineseLetter(string input,int index) { int code = 0; int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend) int chend = Convert.ToInt32("9fff", 16); if (input != "") { code = Char.ConvertToUtf32(input, index); //获得字符串input中指定索引index处字符unicode编码 if (code >= chfrom && code <= chend) { return true; //当code在中文范围内返回true } else { return false ; //当code不在中文范围内返回false } } return false; } 方法二: http://hi.baidu.com/yhfd/blog/item/3222e1fca22cfb80b901a027.html public bool IsChina(string CString) { bool BoolValue = false; for (int i = 0; i < CString.Length; i++) { if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128))) { BoolValue = false; } else { return BoolValue = true; } } return BoolValue; } 方法三: /// /// 判断句子中是否含有中文 宁夏大学 张冬 zd4004.blog.163.com /// /// 字符串 public bool WordsIScn(string words) { string TmmP; for (int i = 0; i < words.Length; i++) { TmmP = words.Substring(i, 1); byte[] sarr = System.Text.Encoding.GetEncoding("gb2312").GetBytes(TmmP); if (sarr.Length == 2) { return true; } } return false; } 方法四: for (int i=0; i< unicodebytearray.length; i++ ) { i++; //如果是中文字符那么高位不为0 if ( unicodebytearray[i] != 0 ) { } …… 方法六 /// /// 给定一个字符串,判断其是否只包含有汉字 /// /// /// public bool IsOnlyContainsChinese(string testStr) { char[] words = testStr.ToCharArray(); foreach (char word in words) { if ( IsGBCode(word.ToString()) || IsGBKCode(word.ToString()) ) // it is a GB2312 or GBK chinese word { continue; } else { return false; } } return true; } /// /// 判断一个word是否为GB2312编码的汉字 /// /// /// private bool IsGBCode(string word) { byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word); if (bytes.Length <= 1) // if there is only one byte, it is ASCII code or other code { return false; } else { byte byte1 = bytes[0]; byte byte2 = bytes[1]; if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254) //判断是否是GB2312 { return true; } else { return false; } } } /// /// 判断一个word是否为GBK编码的汉字 /// /// /// private bool IsGBKCode(string word) { byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(word.ToString()); if (bytes.Length <= 1) // if there is only one byte, it is ASCII code { return false; } else { byte byte1 = bytes[0]; byte byte2 = bytes[1]; if ( byte1 >= 129 && byte1 <= 254 && byte2 >= 64 && byte2 <= 254) //判断是否是GBK编码 { return true; } else { return false; } } } /// /// 判断一个word是否为Big5编码的汉字 /// /// /// private bool IsBig5Code(string word) { byte[] bytes = Encoding.GetEncoding("Big5").GetBytes(word.ToString()); if (bytes.Length <= 1) // if there is only one byte, it is ASCII code { return false; } else { byte byte1 = bytes[0]; byte byte2 = bytes[1]; if ( (byte1 >= 129 && byte1 <= 254) && ((byte2 >= 64 && byte2 <= 126) || (byte2 >= 161 && byte2 <= 254)) ) //判断是否是Big5编码 { return true; } else { return false; } } } 其实原理都一样。 具体的实现,还有几种,就不列举了

在JavaScript中,判断中文字符长度时需要注意,一个中文字符在计算机中通常占用的字节数与英文字符是不同的。大多数的中文字符在UTF-8编码下占用3个字节,而英文字符通常占用1个字节。因此,直接使用字符串的`.length`属性得到的长度并不能真实反映中文字符的数量。为了准确计算中文字符的长度,我们需要编写一个函数,该函数能够区分中文字符和英文字符,并正确计算中文字符的数量。 以下是一个简单的函数示例,用于计算中文字符长度: ```javascript function ChineseCharLength(str) { var len = 0; for (var i = 0; i < str.length; i++) { if (str.charCodeAt(i) > 127) { len++; // 对于中文字符,Unicode值大于127 } } return len; } ``` 这个函数通过遍历字符串中的每个字符,并检查其字符编码来判断是否为中文字符。如果字符的Unicode值大于127,则认为它是一个中文字符。这里需要注意的是,这个判断方法是基于Unicode编码的大致范围,并不是非常严格的判断标准,因为在Unicode编码中,有些特殊符号和表情符号等也可能具有较大的编码值。 如果使用的是正则表达式,可以采用以下方法: ```javascript function ChineseCharLength(str) { return (str.match(/[\u4e00-\u9fa5]/g) || []).length; } ``` 这里使用了正则表达式`/[\u4e00-\u9fa5]/g`,它匹配Unicode编码范围在`\u4e00`到`\u9fa5`之间的中文字符,该范围大致涵盖了常用的中文字符
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值