zoukankan      html  css  js  c++  java
  • 判断字符的类别:数字、字母、汉字、非中文宽字符还是标点符号

      最近在做一个字符串排序的东西,排序的规则有点儿变态,它要求字母、非中文宽字符、中文、数字、标点符号的优先级以此降低,因此我需要知道当前字符到底是什么类别的,查了一些资料,加上自己的一些摸索,写了一个判别方法,求指导:

    首先是一个判断是否为中文的方法,用的是正则表达式:

                public static bool IsChineseCh(string input)
                {
                    Regex regex = new Regex("^[\u4e00-\u9fa5]+$");
                    return regex.IsMatch(input);
                }

    然后是判别方法:

    返回值: 0标示字母,1标示非中文宽字符,2标示中文,3标示数字,4标示标点符号

                private int GetCharRepresent(char ch)
                {
                    if (char.IsPunctuation(ch) || char.IsSeparator(ch) || char.IsSymbol(ch))
                        return 4;
                    if ((int)ch > 255)
                    {
                        string chStr = ch.ToString();
                        if (!IsChineseCh(chStr))
                            return 1;//非中文宽字符
                           else
                            return 2;//中文
                      }
                    else if ((int)ch <= 255)
                    {
                        if (char.IsLetter(ch))
                            return 0;//字母
                           else if (char.IsNumber(ch))
                            return 3;//数字
                           else
                            return 4;//其他字符
                      }
                    else
                        throw new ArgumentException(ch.ToString());
                }
  • 相关阅读:
    sql server中的孤立用户
    BWOA开源项目引子
    海边拾贝
    ABAP syntax_error 错误: form send_cmplx_data_015 does not exist.
    一点总结,手机应用开发前景
    这个周末发生了很多事
    品味单反
    远图(FarMap)使用
    And和手机随想
    远图(FarMap)花絮
  • 原文地址:https://www.cnblogs.com/yuqf/p/get_char_type.html
Copyright © 2011-2022 走看看