1 {
2 Console.WriteLine("请输入字符串");
3 string strA =Console.ReadLine();
4 Console.WriteLine("请输入要查找的字符串");
5 string strB=Console .ReadLine ();
6 string stra = strA.ToLower(); //此处为不区分大小写
7 string strb = strB.ToLower();
8 int len=stra.Length,i,j=0; //i为索引位置,j为计数器
9 List<int> list = new List<int>(); //List用于不确定长度的数组很好用,注意定义方式;List必须定义int、string等,arraylist可以不用设置,但是占用资源较多,用法类似
10 bool isContains = strA .Contains (strb);
11 if (isContains)
12 for (int k = 0; k < len; k++) //k负责穷举索引位置,i为索引位置
13 {
14 i = stra.IndexOf(strb, k);
15 if (i==k)
16 {
17 list.Add(i);
18 j++;
19 }
20 }
21 else
22 Console .WriteLine ("字符串中未包含所需查找的字符串");
23 Console.WriteLine("出现的次数为" + j);
24 Console.Write("出现的索引位置为" );
25 foreach (int x in list) //逐个访问数组中的项
26 {
27 Console.Write(x+" ");
28 }
29 Console.ReadLine ();
30 }