本文转自:https://www.cnblogs.com/willingtolove/p/9782533.html

目录

正文回到顶部

 #方法一:使用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.IndexOf方法

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

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

回到顶部

 #当设计到大小写的问题时,那什么时候使用Contains的上述方法,什么时候使用IndexOf的上述方法,哪个效率高?

  contains方法的源码中其实是使用了IndexOf方法的,但效率还是有差别的;

1、测试代码:基于.net4.5

  注:此测试针对的是拥有大量英文的情况下,并且指定的字符串为英文

  每个方法测试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、测试结果:

3、总结

  1、从测试结果(大量测试)中能明显看出,当拥有大量英文的字符串中:

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

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

  *如果判断的是中文,没有大小写之分,还是string.Contains方法的效率高;

  2、综合上述总结,定义了一个String扩展方法,该方法包含一个StringComparison参数,返回值为是否包含子字符串:

    参考:https://docs.microsoft.com/zh-cn/dotnet/api/system.string.contains?redirectedfrom=MSDN&view=netframework-4.5#System_String_Contains_System_String_

复制代码
 1 using System;
 2 
 3 public static class StringExtensions
 4 {
 5    public static bool Contains(this String str, String substring, 
 6                                StringComparison comp)
 7    {                            
 8       if (substring == null)
 9          throw new ArgumentNullException("substring", 
10                                          "substring cannot be null.");
11       else if (! Enum.IsDefined(typeof(StringComparison), comp))
12          throw new ArgumentException("comp is not a member of StringComparison",
13                                      "comp");
14 
15       return str.IndexOf(substring, comp) >= 0;                      
16    }
17 }
复制代码
复制代码
 1 using System;
 2 
 3 public class Example
 4 {
 5    public static void Main()
 6    {
 7       String s = "This is a string.";
 8       String sub1 = "this";
 9       Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1);
10       StringComparison comp = StringComparison.Ordinal;
11       Console.WriteLine("   {0:G}: {1}", comp, s.Contains(sub1, comp));
12       
13       comp = StringComparison.OrdinalIgnoreCase;
14       Console.WriteLine("   {0:G}: {1}", comp, s.Contains(sub1, comp));
15    }
16 }
17 // The example displays the following output:
18 //       Does 'This is a string.' contain 'this'?
19 //          Ordinal: False
20 //          OrdinalIgnoreCase: True