62,244
社区成员




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