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));