判断是否是中文、英文字符串

Posted on 2019-03-29 23:04  金色的省略号  阅读(919)  评论(0编辑  收藏  举报

 1.判断一个字符串中的一个字符是否是中文

private static bool IsHanZi(string ch)
{
    byte[] byte_len = System.Text.Encoding.Default.GetBytes(ch);
    if (byte_len.Length == 2) { return true; }
    return false;
}
判断字符是否中文还是英文

 2.判断一个字符串中是否含有中文

 1 using System;
 2 
 3 public class Example
 4 {
 5    public static void Main()
 6    {
 7         bool isChinese = false;
 8         string CString = "a.有能力的;出色的";
 9         for (int i = 0; i < CString.Length; i++)
10         {
11             if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1)))< Convert.ToInt32(Convert.ToChar(128)))
12             {
13                 isChinese = false;
14             }
15             else
16             {
17                 isChinese = true;
18                 break;
19             }
20         }
21         Console.WriteLine(isChinese);
22    }
23 }
判断是否是中文字符串

 3.判断一个字符串是否是英文(正则表达式)

string words = "abc";
System.Text.RegularExpressions.Regex reg = new 
System.Text.RegularExpressions.Regex(@"^[A-Za-z]+$");
Console.WriteLine(reg.IsMatch(words));
正则表达式

 4.判断一个字符串是否包含中文

 1 using System;
 2 
 3 public class Example
 4 {
 5    public static void Main()
 6    {
 7         bool isChinese = false;
 8         string CString = "a.有能力的;出色的";
 9         
10         
11         for (int i = 0; i < CString.Length; i++)
12         {
13             string ch = CString[i].ToString();
14             byte[] byte_len = System.Text.Encoding.Default.GetBytes(ch);
15             if (byte_len.Length == 2)        
16             {
17                 isChinese = true;
18                 break;
19             }
20         }
21         Console.WriteLine(isChinese);
22    }
23 }
字符串是否包含中文