C#实现判断字符是否为中文
C#实现判断字符是否为中文
(2012-08-14 14:25:28)
{
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;
}
方法二:
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;
}
方法三:
/// <summary>
/// 判断句子中是否含有中文
/// </summary>
/// <param >字符串</param>
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<s.length; i++)
{
Regex rx = new Regex("^[\u4e00-\u9fa5]$");
if (rx.IsMatch(s[i]))
// 是
else
// 否
}
正解!
\u4e00-\u9fa5 汉字的范围。
^[\u4e00-\u9fa5]$ 汉字的范围的正则
方法五:
unicodeencoding unicodeencoding = new unicodeencoding();
byte [] unicodebytearray = unicodeencoding.getbytes( inputstring );
for( int i = 0; i < unicodebytearray.length; i++ )
{
i++;
//如果是中文字符那么高位不为0
if ( unicodebytearray[i] != 0 )
{
}
……
方法六:
/// <summary>
/// 给定一个字符串,判断其是否只包含有汉字
/// </summary>
/// <param ></param>
/// <returns></returns>
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;
}
/// <summary>
/// 判断一个word是否为GB2312编码的汉字
/// </summary>
/// <param ></param>
/// <returns></returns>
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;
}
}
}
/// <summary>
/// 判断一个word是否为GBK编码的汉字
/// </summary>
/// <param ></param>
/// <returns></returns>
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;
}
}
}
/// <summary>
/// 判断一个word是否为Big5编码的汉字
/// </summary>
/// <param ></param>
/// <returns></returns>
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;
}
}
C#实现判断字符是否为中文的更多相关文章
- Android中判断字符是否为中文、韩文、日文
我们经常需要在程序中判断一个字符是否为CJK(Chinese.Japanese.Korean)语言的字符. 例如,在Contacts里面程序需要判断联系人姓名的所属语言. 今天为大家介绍一种NameS ...
- paip.判断字符是否中文与以及判读是否是汉字uapi python java php
paip.判断字符是否中文与以及判读是否是汉字uapi python java php ##判断中文的原理 注意: 中文与汉字CJKV 的区别..日本,韩国,新加坡,古越南等国家也用汉字,但不是中 ...
- JavaScript判断字符串是否含有中文(实用)
引用页: http://javasam.iteye.com/blog/1465048 UTF-8有点类似于Haffman编码,它将Unicode编码为:0x00-0x7F的字符,用单个字节来表示:0x ...
- C# 判断字符编码的六种方法
方法一http://blog.csdn.net/qiujiahao/archive/2007/08/09/1733169.aspx在unicode 字符串中,中文的范围是在4E00..9FFF:CJK ...
- JavaScript判断是否全为中文,是否含有中文
来源于:http://blog.csdn.net/yenange/article/details/7463897 第一种代码(全为中文则返回"true",不全为中文则返回" ...
- 【转载】C#怎么判断字符是不是汉字
支持并尊重原创!原文地址:http://jingyan.baidu.com/article/2c8c281deb79ed0008252af1.html 判断一个字符是不是汉字通常有三种方法,第1种用 ...
- C#中判断字符是否大写
在C#中,通常判断一个字符是否为大写字母,有些人可能会第一时间想到用正则表达式,那除了正则表达式,是否还有其他方式呢? 答案是肯定的,先一睹为快,具体代码如下: using System; using ...
- js密码的校验(判断字符类型、统计字符类型个数)
/** *判断字符类型 */ function CharMode(iN) { if (iN >= 48 && iN <= 57) //数字 return 1; if (iN ...
- (后端)项目中的错误之java中判断字符里面含有某些字符
数据库的数据出现了数据错误.找到原因是因为代码里面Spring的判断所导致的.其实就是判断字符里有01,走这里,有02,走那里,全是if,但是是类似indexOf的那种判断,偏偏有一个数据是0102, ...
随机推荐
- Openvswitch原理与代码分析(6):用户态流表flow table的操作
当内核无法查找到流表项的时候,则会通过upcall来调用用户态ovs-vswtichd中的flow table. 会调用ofproto-dpif-upcall.c中的udpif_upcall_hand ...
- hexdump—Linux系统的二进制文件查看工具
hexdump 无参: 相当于 hexdump -x 0000000 457f 464c 0102 0001 0000 0000 0000 0000 0000010 0002 003e 0001 00 ...
- neo4j安装与示例
Neo4j有两种访问模式:服务器模式和嵌入模式参考,下面主要讲windows下这两种模式的配置与访问示例 1 Windows下Neo4j服务器模式安装与示例 安装: 1.下载Neo4j,我下载的版本是 ...
- npoi与memcached中的ICSharpCode.SharpZipLib版本冲突的解决方案
项目中一直使用NPOI与memcached,一直相安无事,但是最近升级了npoi到最新版本,发生了ICSharpCode.SharpZipLib的版本冲突问题. 因为此前一直使用的是NPOI的1.x的 ...
- 哈希表用于Key与Value的对应
一个类的某个属性要实现Key与Value的对应,以便通过访问名称就可以知道对应值,而不是通过索引号,最简单的方法直接用 哈希表using System.Collections;class Class1 ...
- 比较下OceanBase的选举协议和Raft的选举协议的区别
阿里技术大讲堂OceanBase专场中曾有专门一场讲座介绍OB自己实现的分布式选举算法:<分布式选举-破解数据库高可用性难题> 这里简单列一下这个选举算法和raft论文中提到的选举算法的区 ...
- wireshark如何抓取本机包
在进行通信开发的过程中,我们往往会把本机既作为客户端又作为服务器端来调试代码,使得本机自己和自己通信.但是wireshark此时是无法抓取到数据包的,需要通过简单的设置才可以. 具体方法如下: 方法一 ...
- [转]relative、absolute和float
position:relative和position:absolute都可以改变元素在文档中的位置,都能激活元素的left.top.right.bottom和z-index属性.(默认这些属性未激活, ...
- MySQL单机load过高问题讨论
有一个朋友问我: "hi,我想问下你们遇到单机load过高的情况 采取什么紧急措施啊?" 我问他是不是mysql db server? 他说是. 我给他如下建议: 1 先看下是不是 ...
- iOS-分段控制器-基本概念
可以直接复制使用 #import "FirstViewController.h" #import "Masonry.h" @interface FirstVie ...