C#实现判断字符是否为中文

C#实现判断字符是否为中文

(2012-08-14 14:25:28)

标签:

gb2312

big5编码

gbk编码

判断

汉字

杂谈

分类: 技术

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;

}
方法二:
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;

}

}

时间: 2024-07-30 08:57:35

C#实现判断字符是否为中文的相关文章

判断字符是否属于中文

1 public class IsChineseOrEnglish { 2 // GENERAL_PUNCTUATION 判断中文的“号 3 // CJK_SYMBOLS_AND_PUNCTUATION 判断中文的.号 4 // HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号 5 public static boolean isChinese(char c) { 6 Character.UnicodeBlock ub = Character.UnicodeBlock.

如何判断字符是不是中文

通过判断字符的编码大小来确定字符  “C”  是否是中文,不同编码中字符编码大小不同. 1.utf-8编码 '\u4e00' <= ch <= '\u9fff' 2.unicode编码 c>=u'\u4e00' and c<=u'\u9fa5' 原文地址:https://www.cnblogs.com/51python/p/10439089.html

android 4.4.3上面,联系人的头像默认显示首字母,但是不支持中文字符,修改支持中文

在android 4.4.3上面,联系人的头像默认显示首字母,但是不支持中文字符,如下图: 如果联系人名字的第一位是英文字符(a-z || A-Z),则默认头像将显示该首字母. 如果支持中文时显示第一个汉字,那就happy了. 那就看看如何通过修改源代码来实现这一小功能吧- 我们还是先了解下联系人头像加载的流程吧- 联系人头像加载这个问题还是很有意思的,在Contacts中使用ContactPhotoManager类(严格来讲是这个类的子类)来实现头像的异步加载. 这个类还使用了LruCache

C# 判断字符编码的六种方法

方法一http://blog.csdn.net/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 ch

js判断字符串是否包含中文或英文

转自 http://yuanliang4521-163-com.iteye.com/blog/1888601 第一种方法 <script language="javascript"> function funcChina(){ var obj = document.form1.txtName.value; if(/.*[\u4e00-\u9fa5]+.*$/.test(obj)) { alert("不能含有汉字!"); return false; } r

Js判断字符的种类

Js判断字符的种类:unicode范围: 48-57:0-9    数字字符 65-90:A-Z    大写字母 97-122: a-z  小写字母 19968-40869:汉字 其他字符 实例:输出一句话中的 汉子.数字.英文字母.特殊字符的个数 <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></ti

PHP判断是否都是中文

 1 /** 2  * 判断是否都是中文 3  * @param $str 4  * @return int 5  */ 6 public static function IsAllChinese($str) 7 { 8     $len = preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$str); 9     if ($len)10     {11         return true;12     }13     return false;14 }

c# 判断字符是否是全角, 获取字符串的字节数 , 获取字符串指定长度字节数的字符串

1 Encoding.Default.GetByteCount(checkString);  =2 全角 =1 半角 /// <summary> /// 获取字符串的字节长度 /// </summary> /// <param name="str"></param> /// <returns></returns> public static int GetStringByteLength(this string s

c语言里面判断字符是否为汉字

这是跟汉字的存储方式有关,西文字符用ASCII码的话,一个字节可以表示一个字符,而汉字用的是双字节表示一个汉字.那么,为了在机器内部区分ASCII码和汉字机内码,就规定汉字的两个字节的最高为都为1.例如:汉字"啊"的汉字机内码为B0A1H表示成双字节是这样的10110000 10100001本程序用的就是判断最高位的方法.int is_zh_ch(char p){ if(~(p >> 8) == 0) 将p字节进行移位运算,右移8位,这样,如果移位后是0,则说明原来的字节最