当前位置: 移动技术网 > IT编程>开发语言>c# > C#判断字符编码的方法总结(六种方法)

C#判断字符编码的方法总结(六种方法)

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例总结了c#判断字符编码的方法。分享给大家供大家参考,具体如下:

方法一

在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;
}

方法二:

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>
/// 判断句子中是否含有中文   宁夏大学 张冬 zd4004.blog.163.com
/// </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;
    }
  }
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网