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

目录 #方法一:使用string.Contains方法 #方法二:使用sring.Index方法 #那什么时候使用Contains方法,什么时候使用Index方法,哪个效率高? 1、测试代码: 2、测试结果: 3、总结 正文 回到顶部 #方法一:使用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、测试结果: 3、总结   从测试结果(大量测试)中能明显看出,当拥有大量英文的字符串中:   当不区分大小写时,string.IndexOf方法的效率明显高于string.Contains方法;   当区分大小写时,string.Contains方法的效率明显高于string.IndexOf方法;   如果判断的是中文,没有大小写之分,还是string.Contains方法的效率高; 作者:willingtolove 出处:http://www.cnblogs.com/willingtolove/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。https://www.cnblogs.com/willingtolove/p/9782533.html
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信