您的位置:首页 > 编程语言 > C#

c# 查询字符串内有多少个指定字符!

2012-02-16 18:53 190 查看
C#计算字符串中有多少个“a”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("这是个计算字符串中含有多少/"a/"的程序");
Console.WriteLine("请输入字符串");

string s1 = Console.ReadLine();
//int x = s1.IndexOf("a");
int count = 0;
int startIndex = 0;
while (true)
{
int y = s1.IndexOf("a", startIndex);
if (y != -1)
{
count++;
startIndex = y + 1;
}
else
{
break;
}
}
Console.WriteLine("/"a/"在字符串中出现了{0}次", count);
Console.ReadLine();
}
}
}


first:

using System.Text;

using System.Text.RegularExpressions;

string a = "adfdf:adfa:afafwer:";

Regex rg = new Regex(":");

MatchCollection mc = rg.Matches(a);

Console.WriteLine(string.Format("Count of @a :{0}", mc.Count));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: