public string CharStat(string str){
int digitCount = 0;
int leterCount = 0;
int spaceCount = 0;
int chineseLeterCount = 0;
int ortherLeterCount = 0;
for (int i=0; i < str.Length; i++)
{
if (Char.IsDigit(str, i))
{
digitCount++; //统计数字
}
else if(Char.IsWhiteSpace(str,i))
{
spaceCount++;//统计空格
}
else if(Char.ConvertToUtf32(str,i) >= Convert.ToInt32("4e00", 16) && Char.ConvertToUtf32(str,i) <= Convert.ToInt32("9fff", 16) )
{
chineseLeterCount ++;//统计汉字
}
else if (Char.IsLetter(str, i))
{
leterCount++;//统计字母,包括汉字
}
else
{
ortherLeterCount++;//其他字符
}
}
return string.Format(str + "中,数字{0}个,字母{1}个,空格{2}个,汉字{3}个,其他字符{4}个", digitCount, leterCount, spaceCount, chineseLeterCount, ortherLeterCount);
本文介绍了一个实用的字符串统计方法,该方法能够精确地统计出给定字符串中的数字、字母、空格、汉字以及其他字符的数量。通过对字符类型的细致划分,帮助开发者更好地理解和处理字符串数据。
1748

被折叠的 条评论
为什么被折叠?



