如何统计一个字符串内有多少个数字和英文字母

yonghot 2012-09-14 05:13:14
在网上找了一个,可能System中没有包含Character这个类,所以,没注意了。
求各位帮忙看一眼。谢谢。
...全文
892 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn_风中雪狼 2012-09-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
正则 试试

C# code

string test = "abCdef12tf356我们都是7";
int count1 = Regex.Matches(test,@"\d").Count;// 数字个数:6
int count2 = Regex.Matches(test, @"(?i)[a-z]").Count;//……
[/Quote]

用正则来实现吧
chen_ya_ping 2012-09-15
  • 打赏
  • 举报
回复

string str = "1ah234b100A";
Regex regex = new Regex("[0-9]");
Console.WriteLine(regex.Matches(str).Count);
Console.WriteLine("-----------------------");
regex = new Regex("[a-z,A-Z]");
Console.WriteLine(regex.Matches(str).Count);
xiedu414 2012-09-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
正则 试试

C# code
string test = "abCdef12tf356我们都是7";
int count1 = Regex.Matches(test,@"\d").Count;// 数字个数:6
int count2 = Regex.Matches(test, @"(?i)[a-z]").Count;//字母……
[/Quote]
+1
haojuntu 2012-09-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

很简单 提供思路 用ascii码判断就好
[/Quote]

用的一楼方法,结果没有问题

//i1字母数量、i2数字数量、i3其它
int i1=0, i2=0, i3=0;
string temp = "abcdefghij852为什么417";
foreach (char item in temp)
{
int i = (int)item;
if (i > 47 && i < 58)
i2++;
else if ((i > 96 && i < 123) || (i > 64 && i < 91))
i1++;
else
i3++;
}
string result = string.Format("字母数量:{0},数字数量:{1},文字数量:{2}",i1,i2,i3);
Console.WriteLine(result);
Console.ReadLine();
threenewbee 2012-09-14
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "hello csdn, hello 404.";
var query = from x in s.ToUpper() group x by x >= 'A' && x <= 'Z' ? 0 : (x >= '0' && x <= '9' ? 1 : 2) into g select new { type = g.Key, count = g.Count() };
Console.WriteLine("字母有{0}个.", query.Where(x => x.type == 0).Single().count);
Console.WriteLine("数字有{0}个.", query.Where(x => x.type == 1).Single().count);
Console.WriteLine("其它有{0}个.", query.Where(x => x.type == 2).Single().count);
}
}
}


字母有14个.
数字有3个.
其它有5个.
Press any key to continue . . .
cloudapex 2012-09-14
  • 打赏
  • 举报
回复

string str = "另顶戴枯aslkdfjasf345sdfa";

char[] arr = str.ToCharArray();

int number = 0;
int english = 0;

foreach (char c in arr)
{
int i = c + 0;

if (i > 47 && i < 58)
number++;
else if ((i > 64 && i < 91) || (i > 96 && i < 123))
english++;
}

Console.WriteLine(string.Format("原字符串:{0}\r\n数字:{1} 个\r\n字母:{2} 个", str, number, english));

Console.Write("\n按任一键结束...");
Console.ReadKey();
  • 打赏
  • 举报
回复
正则 试试
string test = "abCdef12tf356我们都是7";
int count1 = Regex.Matches(test,@"\d").Count;// 数字个数:6
int count2 = Regex.Matches(test, @"(?i)[a-z]").Count;//字母个数:8
int count3 = Regex.Matches(test, @"[\u4e00-\u9fa5]").Count;//汉字个数:4
threenewbee 2012-09-14
  • 打赏
  • 举报
回复
string s = "hello csdn, hello 404.";
var query = from x in s.ToUpper() group x by x >= 'A' && x <= 'Z' ? 0 : (x >= '0' && x <= '9' ? 1 : 2) into g select new { type = g.Key, count = g.Count() };
Console.WriteLine("字母有{0}个", query.Where(x => x.Type == 0).Single().count);
Console.WriteLine("数字有{0}个", query.Where(x => x.Type == 1).Single().count);
Console.WriteLine("其它有{0}个", query.Where(x => x.Type == 2).Single().count);
Banianer 2012-09-14
  • 打赏
  • 举报
回复
你对字符做一个单个截取的循环,判断该字符是否在 ascii码 数字和英文的范围之内,如果是
则统计+1 最后输出统计变量。
PandaIT 2012-09-14
  • 打赏
  • 举报
回复
很简单 提供思路 用ascii码判断就好

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧