获取字符串中的数字、符号、中文、英文单词、字母、空格、字节、其他字符的个数

获取字符串中的数字、符号、中文、英文单词、字母、空格、字节、其他字符的个数

标签: 

it

分类: java

//英文单词:根据正则获取
        private static int GetWordCountByRegular(string str)
        {
            //统计英文单词个数
            Regex re = new Regex(@"\b\w+\b");
            MatchCollection ma = re.Matches(str);
            return ma.Count;
        }


        //数字
        public static int GetNumberCount(string str)
        {
            int count = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != '\0')
                {
                    if (str[i] >= '0' && str[i] <= '9')
                    {
                        count++;
                    }
                }
            }
            return count;
        }

        //字母
        public static int GetLetterCount(string str)
        {
            int count = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != '\0')
                {
                    if (((str[i] >= 'a' && str[i] <= 'z')) || ((str[i] >= 'A' && str[i] <= 'Z')))
                    {
                        count++;
                    }
                }
            }
            return count;
        }

        //中文字符
        public static int GetChineseCount(String str)
        {
            //中文个数=字节数-字符数
            return Encoding.GetEncoding("gb2312").GetBytes(str).Length - str.Length;
        }

        //中文字符:根据Unicode编码范围
        private static int GetChineseCountByUnicode(string str)
        {
            int count = 0;

            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] >= 0X4e00 && str[i] <= 0X9fa5)
                {
                    count++;
                }
            }
            return count;
        }

        //中文字符:根据正则获取
        private static int GetChineseCountByRegular(String str)
        {
            Regex re = new Regex("[\u4e00-\u9fa5]");
            MatchCollection ma = re.Matches(str);
            return ma.Count;
        }

        //空格
        public static int GetSpaceCount(String str)
        {
            int count = 0;
            foreach (char ch in str)
            {
                if (ch == 32) //ASCII编码:32为空格符.当然你也可以判断空字符:ch==' '
                {
                    count++;
                }
            }

            return count;
        }

        //标点符号
        public static int GetSymbolCount(String str)
        {
            //ASCII编码中的符号范围:32-47、58-64、91-96、123-126
            int count = 0;
            foreach (char ch in str)
            {
                if ((ch >= 32 && ch <= 47) || (ch >= 58 && ch <= 64) || (ch >= 91 && ch <= 96) || (ch >= 123 && ch <= 126))
                {
                    count++;
                }
            }

            return count;
        }

        //其他字符
        public static int GetOtherCount(String str)
        {
            return str.Length - GetNumberCount(str) - GetLetterCount(str) - GetChineseCount(str)
                - GetSpaceCount(str) - GetSymbolCount(str);
        }

        //字节
        public static int GetByteCount(String str)
        {
            return Encoding.Default.GetBytes(str).Length;
        }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值