zoukankan      html  css  js  c++  java
  • 判断是否是中文、英文字符串

     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 }
    字符串是否包含中文
  • 相关阅读:
    P3350 [ZJOI2016]旅行者
    P4178 Tree
    P2375 [NOI2014]动物园
    P2827 蚯蚓
    1002: [FJOI2007]轮状病毒
    1070: [SCOI2007]修车
    AtCoder Grand Contest 021完整题解
    Running to the End(Codeforces & AtCoder 百套计划)
    SDWC2017游记
    非传统题初探——AtCoder Practice Contest #B
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10624749.html
Copyright © 2011-2022 走看看