/// <summary>
/// 给定一个字符串,判断其是否只包含有汉字
/// </summary>
/// <param ></param>
/// <returns></returns>
public static bool IsChinese(string str)
{
Regex rx = new Regex("^[\u4e00-\u9fa5]$");
for (int i = 0; i < str.Length; i++)
{
if (rx.IsMatch(str.Substring(i, 1)))
{
continue;
}
else
{
return false;
}
}
return true;
}
/// <summary>
/// 判断一个word是否为GB2312编码的汉字
/// </summary>
/// <param ></param>
/// <returns></returns>
private static 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 static 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;
}
}
}