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 }
    字符串是否包含中文
  • 相关阅读:
    CSS cursor 属性笔记
    sql 不等于 <>
    去掉时间中的时分秒
    ref 和 out区别
    关于闭包(未完待续)
    面向对象——多态(摘)
    SQL Service 数据库 基本操作 视图 触发器 游标 存储过程
    遍历winform 页面上所有的textbox控价并赋值string.Empty
    关于Html 和Xml 区别(备忘)
    python之面向对象进阶
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10624749.html
Copyright © 2011-2022 走看看