判断字符的类别:数字、字母、汉字、非中文宽字符还是标点符号

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

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

            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());
            }

 

posted @ 2013-03-26 11:46  菜鸟_飞飞  阅读(1291)  评论(0编辑  收藏  举报