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

 更新时间:2016年06月13日 09:27:12   作者:郑文亮  
这篇文章主要介绍了C#判断字符编码的方法,结合实例形式总结分析了六种C#判断字符编码的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例总结了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#程序设计有所帮助。

相关文章

  • Chrome Visual Studio 2005下的编译过程

    Chrome Visual Studio 2005下的编译过程

    研究Chrome ,首先得把它编译出来,这对于后续的代码分析和阅读有很大的帮助,想想自己编译出一个 Chrome 浏览器来使用,那是一件很炫的事情。
    2009-07-07
  • C# Pointer指针应用实例简述

    C# Pointer指针应用实例简述

    这篇文章主要介绍了C# Pointer指针应用,对初学者很有借鉴学习价值,需要的朋友可以参考下
    2014-08-08
  • C# 微信支付回调验签处理的实现

    C# 微信支付回调验签处理的实现

    在微信支付中,当用户支付成功后,微信会把相关支付结果和用户信息发送给商户,本文就详细的介绍了C# 微信支付回调验签处理,具有一定的参考价值,感兴趣的可以了解一下
    2021-12-12
  • C#无损高质量压缩图片代码

    C#无损高质量压缩图片代码

    这篇文章主要为大家详细介绍了C#无损高质量压缩图片代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • C# Directory.GetFiles()函数案例详解

    C# Directory.GetFiles()函数案例详解

    这篇文章主要介绍了C# Directory.GetFiles()函数案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • c#创建浮动工具栏功能示例

    c#创建浮动工具栏功能示例

    这篇文章主要介绍了c#创建浮动工具栏功能示例,大家参考使用吧
    2013-12-12
  • C#逐行读取文本文件的几种有效方法

    C#逐行读取文本文件的几种有效方法

    在C#中,我们可以使用各种方法来读取和写入文本文件,本文主要给大家介绍了C#逐行读取文本文件的几种有效方法,并通过代码示例讲解的非常详细,需要的朋友可以参考下
    2025-01-01
  • 无焦点获取条码枪返回值示例

    无焦点获取条码枪返回值示例

    这篇文章主要介绍了使用C#获取条码枪的返回值,实现了获取条码信息、条码是否有效、扫描时间、扫描码等参数
    2014-01-01
  • c#结构和类的相关介绍

    c#结构和类的相关介绍

    结构和类的共同点都是属于抽象数据类型,包含数据和数据的操作。不同点在于结构偏重于数据语意,而类偏重於行为语意。
    2012-12-12

最新评论