国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#判斷字符編碼的方法總結(jié)(六種方法)

2019-10-29 21:26:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例總結(jié)了C#判斷字符編碼的方法。分享給大家供大家參考,具體如下:

方法一

在unicode 字符串中,中文的范圍是在4E00..9FFF:CJK Unified Ideographs。
通過(guò)對(duì)字符的unicode編碼進(jìn)行判斷來(lái)確定字符是否為中文。

protected bool  IsChineseLetter(string input,int index){    int code = 0;    int chfrom = Convert.ToInt32("4e00", 16);  //范圍(0x4e00~0x9fff)轉(zhuǎn)換成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;   //當(dāng)code在中文范圍內(nèi)返回true       }      else      {         return false ;  //當(dāng)code不在中文范圍內(nèi)返回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>/// 判斷句子中是否含有中文   寧夏大學(xué) 張冬 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++;//如果是中文字符那么高位不為0if ( unicodebytearray[i] != 0 ){}……

方法六

/// <summary>/// 給定一個(gè)字符串,判斷其是否只包含有漢字/// </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>/// 判斷一個(gè)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>/// 判斷一個(gè)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>/// 判斷一個(gè)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;    }  }}


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到c#教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 盐城市| 正宁县| 泾川县| 苏尼特右旗| 马鞍山市| 青冈县| 阿拉善盟| 桃园市| 涟源市| 关岭| 商河县| 克拉玛依市| 县级市| 乌拉特后旗| 长沙市| 土默特右旗| 义乌市| 手机| 萝北县| 石嘴山市| 金溪县| 平罗县| 鄂伦春自治旗| 新闻| 峨山| 库尔勒市| 青冈县| 林口县| 镇平县| 凤山县| 黄大仙区| 花莲市| 库车县| 麻阳| 缙云县| 娄烦县| 凤阳县| 柳江县| 吴忠市| 镇宁| 鹤峰县|