码迷,mamicode.com
首页 > 其他好文 > 详细

字符串中查询关键字

时间:2015-08-07 09:32:21      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

 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         }

 

字符串中查询关键字

标签:

原文地址:http://www.cnblogs.com/ck235/p/4709737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!