Chinaunix首页 | 论坛 | 博客
  • 博客访问: 162429
  • 博文数量: 26
  • 博客积分: 1225
  • 博客等级: 中尉
  • 技术积分: 395
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-06 21:11
文章分类

全部博文(26)

文章存档

2018年(2)

2014年(1)

2011年(3)

2010年(2)

2009年(1)

2008年(17)

我的朋友

分类: WINDOWS

2008-08-13 18:26:28

方法一: 在unicode 字符串中,中文的范围是在4E00..9FFF:CJK Unified Ideographs。 通过对字符的unicode编码进行判断来确定字符是否为中文。 程序代码:

  1. protected bool   IsChineseLetter(string input,int index)   
  2. {   
  3.          int code = 0;   
  4.          int chfrom = Convert.ToInt32("4e00", 16);     //范围(0x4e00~0x9fff)转换成int(chfrom~chend)   
  5.          int chend = Convert.ToInt32("9fff", 16);   
  6.          if (input != "")   
  7.          {   
  8.              code = Char.ConvertToUtf32(input, index);     //获得字符串input中指定索引index处字符unicode编码   
  9.   
  10.            if (code >= chfrom && code <= chend)        
  11.              {   
  12.                  return true;     //当code在中文范围内返回true   
  13.              }   
  14.              else  
  15.              {   
  16.                  return false ;     //当code不在中文范围内返回false   
  17.              }   
  18.          }   
  19.           return false;   
  20. }

方法二: 程序代码:

  1. public bool IsChina(string CString)   
  2. {   
  3.     bool BoolValue = false;   
  4.   
  5.     for (int i = 0; i < CString.Length; i++)   
  6.     {   
  7.         if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))   
  8.         {   
  9.             BoolValue = false;   
  10.         }   
  11.         else  
  12.         {   
  13.             return BoolValue = true;   
  14.         }   
  15.     }   
  16.   
  17.     return BoolValue;   
  18. }  

方法三: 程序代码:

  1.          ///    
  2.          /// 判断句子中是否含有中文   
  3.          ///    
  4.          /// 字符串   
  5.          public bool WordsIScn(string words)   
  6.          {   
  7.              string TmmP;   
  8.   
  9.              for (int i = 0; i < words.Length; i++)   
  10.              {   
  11.                  TmmP = words.Substring(i, 1);   
  12.   
  13.                  byte[] sarr = System.Text.Encoding.GetEncoding("gb2312").GetBytes(TmmP);   
  14.   
  15.                  if (sarr.Length == 2)   
  16.                  {   
  17.                      return true;   
  18.                  }   
  19.              }   
  20.              return false;   
  21.          }  

方法四: 程序代码:  

  1. for (int i=0; i
  2. {   
  3.     Regex rx = new Regex("^[\u4e00-\u9fa5]___FCKpd___4quot;);   
  4.   
  5.     if (rx.IsMatch(s[i]))   
  6.         // 是   
  7.     else  
  8.        // 否   
  9. }
 \u4e00-\u9fa5 汉字的范围。 ^[\u4e00-\u9fa5]$ 汉字的范围的正则
同理可以判断日文: \u0x3040-\u0x309F 是平假名, \u0x30A0-\u0x30FF 是片假名
 
方法五: 程序代码:
  1. unicodeencoding   unicodeencoding   =   new   unicodeencoding();     
  2. byte   []   unicodebytearray   =   unicodeencoding.getbytes(   inputstring   );     
  3. for(   int   i   =   0;   i   <   unicodebytearray.length;   i++   )     
  4. {     
  5.     i++;     
  6.     //如果是中文字符那么高位不为0     
  7.     if   (   unicodebytearray[i]   !=   0   )     
  8.     {     }     
  9.     ……    

方法六: 程序代码:

  1. ///      
  2. /// 给定一个字符串,判断其是否只包含有汉字     
  3. /// 
  4.  
        
  5. ///      
  6. ///      
  7. public bool IsOnlyContainsChinese(string testStr)     
  8. {     
  9.     char[] words = testStr.ToCharArray();   
  10.   
  11.     foreach (char word in words)     
  12.     {     
  13.         if ( IsGBCode(word.ToString()) || IsGBKCode(word.ToString()) )   // it is a GB2312 or GBK chinese word    
  14.         {     
  15.             continue;     
  16.         }     
  17.         else    
  18.         {     
  19.             return false;     
  20.         }     
  21.     }     
  22.     return true;     
  23. }    
  24.   
  25. ///      
  26. /// 判断一个word是否为GB2312编码的汉字     
  27. /// 
  28.  
        
  29. ///      
  30. ///      
  31. private bool IsGBCode(string word)     
  32. {     
  33.     byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word);   
  34.   
  35.     if (bytes.Length <= 1)   // if there is only one byte, it is ASCII code or other code    
  36.     {     
  37.         return false;     
  38.     }     
  39.     else    
  40.     {     
  41.         byte byte1 = bytes[0];     
  42.         byte byte2 = bytes[1];   
  43.   
  44.         if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254)     //判断是否是GB2312   
  45.         {   
  46.             return true;    
  47.         }     
  48.         else    
  49.         {     
  50.             return false;     
  51.         }     
  52.     }     
  53. }     
  54.   
  55. ///      
  56. /// 判断一个word是否为GBK编码的汉字  
  57. /// 
  58.  
        
  59. ///    
  60. ///    
  61. private bool IsGBKCode(string word)   
  62. {   
  63.     byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(word.ToString());   
  64.   
  65.     if (bytes.Length <= 1)   // if there is only one byte, it is ASCII code   
  66.     {   
  67.         return false;   
  68.     }   
  69.     else  
  70.     {   
  71.         byte byte1 = bytes[0];   
  72.         byte byte2 = bytes[1];   
  73.   
  74.         if ( byte1 >= 129 && byte1 <= 254 && byte2 >= 64 && byte2 <= 254)     //判断是否是GBK编码   
  75.         {   
  76.             return true;   
  77.         }   
  78.         else  
  79.         {   
  80.             return false;   
  81.         }
  82.     }   
  83. }   
  84.   
  85. ///    
  86. /// 判断一个word是否为Big5编码的汉字   
  87. /// 
  88.  
      
  89. ///    
  90. ///    
  91.   
  92. private bool IsBig5Code(string word)   
  93. {   
  94.     byte[] bytes = Encoding.GetEncoding("Big5").GetBytes(word.ToString());   
  95.   
  96.     if (bytes.Length <= 1)   // if there is only one byte, it is ASCII code   
  97.     {   
  98.         return false;   
  99.     }   
  100.     else  
  101.     {   
  102.         byte byte1 = bytes[0];   
  103.         byte byte2 = bytes[1];   
  104.   
  105.         if ( (byte1 >= 129 && byte1 <= 254) && ((byte2 >= 64 && byte2 <= 126) || (byte2 >= 161 && byte2 <= 254)) )     //判断是否是Big5编码   
  106.         {   
  107.             return true;   
  108.         }   
  109.         else  
  110.         {   
  111.             return false;   
  112.         }   
  113.     }   
  114. }  
阅读(2191) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

yur5052008-12-08 08:37:16

我把这里作为自己的备忘录了,不好意思啊。 以后有时间会总结自己的东西的。呵呵

chinaunix网友2008-11-07 12:50:09

不知羞耻,到处抄。