#方法一:使用string.Contains方法

  string.Contains是大小写敏感的,如果要用该方法来判断一个string是否包含某个关键字keyword,需要把这个string和这个keyword都转成小写或大写再调用Contains方法;

1 string key = "bbb";
2 string temp = "aaaBBBcccDDD";
3 bool isContains= temp.ToLower().Contains(key.ToLower());//true

#方法二:使用sring.Index方法

  使用string.Index方法,然后通过StringComparison.OrdinalIgnoreCase指定查找过程忽略大小写;

1 string key = "bbb";
2 string temp = "aaaBBBcccDDD";
3 bool isContains = temp.IndexOf(key,StringComparison.OrdinalIgnoreCase)>=0;//true

 #那什么时候使用Contains方法,什么使用Index方法,哪个效率高?

1、测试代码:

  每个方法测试1千万次,输出所用时间;

 1 class Program
 2     {
 3         private const int N = 10000000;
 4         private static Stopwatch watch = new Stopwatch();
 5         static void Main(string[] args)
 6         {
 7
 8             string source = "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooopppqqq";
 9             string target = "AAA";
10             Console.WriteLine("目标在开头部分时:");
11             Console.WriteLine("不区分大小写:");
12             TestContains(source, target,true);
13             TestIndexOf(source, target,true);
14             Console.WriteLine("区分大小写:");
15             target = "aaa";
16             TestContains(source, target,false);
17             TestIndexOf(source, target,false);
18             Console.WriteLine();
19
20             Console.WriteLine("目标在中部时:");
21             Console.WriteLine("不区分大小写:");
22             target = "HHH";
23             TestContains(source, target, true);
24             TestIndexOf(source, target, true);
25             Console.WriteLine("区分大小写:");
26             target = "hhh";
27             TestContains(source, target, false);
28             TestIndexOf(source, target, false);
29             Console.WriteLine();
30
31             Console.WriteLine("目标在结尾时:");
32             Console.WriteLine("不区分大小写:");
33             target = "QQQ";
34             TestContains(source, target,true);
35             TestIndexOf(source, target,true);
36             Console.WriteLine("区分大小写:");
37             target = "qqq";
38             TestContains(source, target,false);
39             TestIndexOf(source, target,false);
40
41             Console.WriteLine("执行完毕,按任意键退出...");
42             Console.ReadKey();
43
44         }
45         private static void TestIndexOf(string source, string target,bool isIgnoreCase)
46         {
47             watch.Reset();
48             watch.Start();
49             for (int i = 0; i < N; i++)
50             {
51                 if (isIgnoreCase)
52                     source.IndexOf(target, StringComparison.OrdinalIgnoreCase);
53                 else
54                     source.IndexOf(target);
55             }
56             watch.Stop();
57             Console.WriteLine("IndexOf: " + watch.ElapsedMilliseconds.ToString() + "ms");
58             return;
59         }
60
61         private static void TestContains(string source, string target,bool isIgnoreCase)
62         {
63             watch.Reset();
64             watch.Start();
65             for (int i = 0; i < N; i++)
66             {
67                 if (isIgnoreCase)
68                     source.ToLower().Contains(target.ToLower());
69                 else
70                     source.Contains(target);
71             }
72             watch.Stop();
73             Console.WriteLine("Contains: " + watch.ElapsedMilliseconds.ToString() + "ms");
74             return;
75         }
76     }

2、测试结果:

【C#】判断字符串中是否包含指定字符串,contains与indexof方法效率问题-LMLPHP

3、总结

  从测试结果(大量测试)中能明显看出:

  当不区分大小写时,string.IndexOf方法的效率明显高于string.Contains方法;

  当区分大小写时,string.Contains方法的效率明显高于string.IndexOf方法;

10-13 15:19