string text = "患者:“大夫,我咳嗽得很重。” 大夫:“你年纪多大?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗?” 患者:“不咳嗽。” 大夫:“四十岁咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时候咳嗽?” ";
int count=0;
int index = 0;
string str="咳嗽";
//text.IndexOf(str, index);
while ((index = text.IndexOf(str, index)) != -1)
{
count++;
Console.WriteLine("第{0}次,出现“咳嗽”,索引是:{1}",count,index);
index = index + str.Length;
}
Console.WriteLine("==========================================================");
Console.WriteLine("咳嗽一词,共出现了{0}次!",count);
char[] arr = text.ToCharArray();
Array.Sort(arr);
string temp = new string(arr);
// Console.WriteLine(temp);
for (int i = 0; i < text.Length; )
{
string c = temp.Substring(i, 1);
Console.WriteLine("{0}{1}", c,temp.LastIndexOf(c)+1-i);
i = temp.LastIndexOf(c)+1;
}
Console.ReadKey();