zoukankan      html  css  js  c++  java
  • C#判断字符串的是否是汉字

      //第一种方法:正则表达式
            private bool IsChinese(string Text)
            {
                for (int i = 0; i < Text.Length; i++)
                {
                    if (Regex.IsMatch(Text.ToString(), @"[u4E00-u9FA5]+$"))
                    {
                        return true;
                        //MessageBox.Show("是汉字");
                    }
                    //else
                    //    MessageBox.Show("不是汉字");
                }
                return false;
    
            }
            //第二种方法:汉字的 UNICODE 编码范围
            private bool IsChinese1(string Text)
            {
                string text = Text;
                char[] c = text.ToCharArray();
                for (int i = 0; i < c.Length; i++)
                    if ((int)c[i] >= 0x4e00 && (int)c[i] <= 0x9fbb)
                        return true;
    
                return false;
    
            }
            //在 ASCII码表中,英文的范围是0-127,而汉字则是大于127
            private bool IsChinese2(string Text)
            {
                string text = Text;
                char[] c = text.ToCharArray();
                for (int i = 0; i < text.Length; i++)
                {
                    if ((int)c[i] > 127)
                    {
                        return true;
                        //Console.WriteLine("是汉字");
                    }
                    //else
                    //    Console.WriteLine("不是汉字");
                }
                return false;
    
            }
            private void button2_Click(object sender, EventArgs e)
            {
                string text = this.textBox1.Text;
                if (IsChinese2(text))
                {
                    MessageBox.Show(text + "中有汉字");
                }
                else
                    MessageBox.Show(text + "中没有汉字");
    
            }
  • 相关阅读:
    go 基本包
    go 包
    算法笔记--数据结构--链表
    算法笔记--数据结构--队列
    算法笔记--标准模板库STL--pair
    算法笔记--标准模板库STL--stack
    算法笔记--标准模板库STL--priority_queue
    算法笔记--标准模板库STL--queue
    初识pair
    lower_bound实现离散化
  • 原文地址:https://www.cnblogs.com/gisoracle/p/8038500.html
Copyright © 2011-2022 走看看