C# 判断中文字符的8种方法

摘要:
通过对字符的unicode编码进行判断来确定字符是否为中文。="")...{code=Char.ConvertToUtf32;//获得字符串input中指定索引index处字符unicode编码if...{returntrue;//当code在中文范围内返回true}else...{returnfalse;//当code不在中文范围内返回false}}returnfalse;}方法二:publicboolIsChina{boolBoolValue=false;for{if{BoolValue=false;}else{returnBoolValue=true;}}returnBoolValue;}方法三://////判断句子中是否含有中文//////字符串publicboolWordsIScn{stringTmmP;for{TmmP=words.Substring(i,1);byte[]sarr=System.Text.Encoding.GetEncoding.GetBytes;if{returntrue;}}returnfalse;}方法四:for{Regexrx=newRegex;if//是else//否}正解!^[\u4e00-\u9fa5]$汉字的范围的正则方法五unicodeencodingunicodeencoding=newunicodeencoding();byte[]unicodebytearray=unicodeencoding.getbytes;for{i++;//如果是中文字符那么高位不为0if(unicodebytearray[i]!

方法一
在unicode 字符串中,中文的范围是在4E00..9FFF:CJK Unified Ideographs。

通过对字符的unicode编码进行判断来确定字符是否为中文。

C# 判断中文字符的8种方法第1张protectedbool IsChineseLetter(string input,int index)
C# 判断中文字符的8种方法第2张C# 判断中文字符的8种方法第3张
...{
C# 判断中文字符的8种方法第4张intcode =0;
C# 判断中文字符的8种方法第4张
intchfrom =Convert.ToInt32("4e00", 16);//范围(0x4e00~0x9fff)转换成int(chfrom~chend)
C# 判断中文字符的8种方法第4张
intchend =Convert.ToInt32("9fff", 16);
C# 判断中文字符的8种方法第4张
if(input !="")
C# 判断中文字符的8种方法第8张C# 判断中文字符的8种方法第9张
...{
C# 判断中文字符的8种方法第4张 code
=Char.ConvertToUtf32(input, index);//获得字符串input中指定索引index处字符unicode编码
C# 判断中文字符的8种方法第4张

C# 判断中文字符的8种方法第4张
if(code >=chfrom &&code <=chend)
C# 判断中文字符的8种方法第8张C# 判断中文字符的8种方法第9张
...{
C# 判断中文字符的8种方法第4张 return true;
//当code在中文范围内返回true
C# 判断中文字符的8种方法第4张

C# 判断中文字符的8种方法第17张 }

C# 判断中文字符的8种方法第4张
else
C# 判断中文字符的8种方法第8张C# 判断中文字符的8种方法第9张
...{
C# 判断中文字符的8种方法第4张 return false ;
//当code不在中文范围内返回falseC# 判断中文字符的8种方法第4张
C# 判断中文字符的8种方法第17张 }

C# 判断中文字符的8种方法第17张 }

return false;
C# 判断中文字符的8种方法第25张 }
方法二:
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 name="testStr"></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 name="word"></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 name="word"></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 name="word"></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;
}
else
{
return false;
}
}
}

上面是前辈们留下的,我想偷点懒。

方法七

引用从微软亚洲语言开发包里的Simplified Chinese Pin-Yin Conversion Library 里提取ChnCharInfo.dll后
using Microsoft.International.Converters.PinYinConverter;
private bool IsHanzi(Char c)
{
return ChineseChar.IsValidChar(c);
}

方法八

引用从微软亚洲语言开发包里的Simplified Chinese Pin-Yin Conversion Library 里提取ChnCharInfo.dll后
using Microsoft.International.Converters.PinYinConverter;
private bool IsHanzi(Char c)
{
try
{
ChineseChar CChar1 = new ChineseChar(c);
return ture;
}
catch
{
return false;
}
}

附:有了这个开发,我这样的菜鸟也可以很容易就玩转汉字游戏.

C# 判断中文字符的8种方法第26张

免责声明:文章转载自《C# 判断中文字符的8种方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vim学习3-《Vim实用技巧》Git建立独立分支下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python(一):python语言基础

一、python语言基本的8个要素 Python语言的8个要素:数据类型、对象引用、组合数据类型、逻辑操作符、运算操作符、控制流语句、输入/输出、函数的创建与引用。除此之外还有一个非常重要且无处不在的要素:对象。实际上,在所有的语言当中,这种要素结构几乎一样。   1.要素1--数据和对象类型 python的数据类型是指内置数据类型。python基本的内置...

Windows下的监控IO性能

在Windows中监视性能基本都用性能监视器了,与IO性能相关的有两个大类,一个是”LogicalDisk”,另外一个是”PhysicalDisk”,”LogicalDisk”更多的是用来监视文件相关的IO性能,而”PhysicalDisk”则是用来监视LUN或者是磁盘卷,下面就列举下与前面所列举的IO性能相关的计数器。了: 单次IO大小 Avg. D...

[转]Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误-高级

from : https://levy.at/blog/11 进阶篇 关闭HTTP的响应 level: intermediate 当你使用标准http库发起请求时,你得到一个http的响应变量。如果你不读取响应主体,你依旧需要关闭它。注意对于空的响应你也一定要这么做。对于新的Go开发者而言,这个很容易就会忘掉。 一些新的Go开发者确实尝试关闭响应主体,但...

Modbus协议服务端(Netty)

pom.xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.0.21.Final...

使用Jacob与Word文件交互

转自:http://www.blogjava.net/lonsy/archive/2009/01/09/250713.html Jacob项目的官方地址: Http://sourceforge.net/projects/jacob-project/官方介绍:    JACOB is a JAVA-COM Bridge that allows you to...

磁盘的分区、格式化、与挂载

1、磁盘分区:fdisk 【-l】 设备名称 -l:输出后面接的设备所有的分区内容。 1 [root@iZ255cppmtxZ ~]# fdisk -l 2 3 Disk /dev/xvda: 42.9 GB, 42949672960 bytes, 83886080sectors 4 Units = sectors of 1 * 512 = 512byt...