C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題
正文
#方法一:使用string.Contains方法
string.Contains是大小寫敏感的,如果要用該方法來判斷一個(gè)string是否包含某個(gè)關(guān)鍵字keyword,需要把這個(gè)string和這個(gè)keyword都轉(zhuǎn)成小寫或大寫再調(diào)用Contains方法;
string key = "bbb"; string temp = "aaaBBBcccDDD"; bool isContains= temp.ToLower().Contains(key.ToLower());//true
#方法二:使用sring.Index方法
使用string.Index方法,然后通過StringComparison.OrdinalIgnoreCase
指定查找過程忽略大小寫;
string key = "bbb"; string temp = "aaaBBBcccDDD"; bool isContains = temp.IndexOf(key,StringComparison.OrdinalIgnoreCase)>=0;//true
#那什么時(shí)候使用Contains方法,什么時(shí)候使用Index方法,哪個(gè)效率高?
1、測(cè)試代碼:
注:此測(cè)試針對(duì)的是擁有大量英文的情況下,并且指定的字符串為英文
每個(gè)方法測(cè)試1千萬次,輸出所用時(shí)間;
class Program { private const int N = 10000000; private static Stopwatch watch = new Stopwatch(); static void Main(string[] args) { string source = "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooopppqqq"; string target = "AAA"; Console.WriteLine("目標(biāo)在開頭部分時(shí):"); Console.WriteLine("不區(qū)分大小寫:"); TestContains(source, target,true); TestIndexOf(source, target,true); Console.WriteLine("區(qū)分大小寫:"); target = "aaa"; TestContains(source, target,false); TestIndexOf(source, target,false); Console.WriteLine(); Console.WriteLine("目標(biāo)在中部時(shí):"); Console.WriteLine("不區(qū)分大小寫:"); target = "HHH"; TestContains(source, target, true); TestIndexOf(source, target, true); Console.WriteLine("區(qū)分大小寫:"); target = "hhh"; TestContains(source, target, false); TestIndexOf(source, target, false); Console.WriteLine(); Console.WriteLine("目標(biāo)在結(jié)尾時(shí):"); Console.WriteLine("不區(qū)分大小寫:"); target = "QQQ"; TestContains(source, target,true); TestIndexOf(source, target,true); Console.WriteLine("區(qū)分大小寫:"); target = "qqq"; TestContains(source, target,false); TestIndexOf(source, target,false); Console.WriteLine("執(zhí)行完畢,按任意鍵退出..."); Console.ReadKey(); } private static void TestIndexOf(string source, string target,bool isIgnoreCase) { watch.Reset(); watch.Start(); for (int i = 0; i < N; i++) { if (isIgnoreCase) source.IndexOf(target, StringComparison.OrdinalIgnoreCase); else source.IndexOf(target); } watch.Stop(); Console.WriteLine("IndexOf: " + watch.ElapsedMilliseconds.ToString() + "ms"); return; } private static void TestContains(string source, string target,bool isIgnoreCase) { watch.Reset(); watch.Start(); for (int i = 0; i < N; i++) { if (isIgnoreCase) source.ToLower().Contains(target.ToLower()); else source.Contains(target); } watch.Stop(); Console.WriteLine("Contains: " + watch.ElapsedMilliseconds.ToString() + "ms"); return; } }
2、測(cè)試結(jié)果:
3、總結(jié)
1、從測(cè)試結(jié)果(大量測(cè)試)中能明顯看出,當(dāng)擁有大量英文的字符串中:
*當(dāng)不區(qū)分大小寫時(shí),string.IndexOf方法的效率明顯高于string.Contains方法;
*當(dāng)區(qū)分大小寫時(shí),string.Contains方法的效率明顯高于string.IndexOf方法;
*如果判斷的是中文,沒有大小寫之分,還是string.Contains方法的效率高;
2、綜合上述總結(jié),定義了一個(gè)String擴(kuò)展方法,該方法包含一個(gè)StringComparison參數(shù),返回值為是否包含子字符串:
參考:https://docs.microsoft.com/zh-cn/dotnet/api/system.string.contains?redirectedfrom=MSDN&view=netframework-4.5#System_String_Contains_System_String_
using System; public static class StringExtensions { public static bool Contains(this String str, String substring, StringComparison comp) { if (substring == null) throw new ArgumentNullException("substring", "substring cannot be null."); else if (! Enum.IsDefined(typeof(StringComparison), comp)) throw new ArgumentException("comp is not a member of StringComparison", "comp"); return str.IndexOf(substring, comp) >= ; } } using System; public class Example { public static void Main() { String s = "This is a string."; String sub1 = "this"; Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1); StringComparison comp = StringComparison.Ordinal; Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp)); comp = StringComparison.OrdinalIgnoreCase; Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp)); } } // The example displays the following output: // Does 'This is a string.' contain 'this'? // Ordinal: False // OrdinalIgnoreCase: True
總結(jié)
以上所述是小編給大家介紹的C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
上一篇:C# TextBox數(shù)據(jù)綁定的方法
欄 目:C#教程
下一篇:C#簡(jiǎn)單實(shí)現(xiàn)發(fā)送socket字符串
本文標(biāo)題:C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5069.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)實(shí)體類與字符串互相轉(zhuǎn)換的方法
- 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10C#實(shí)現(xiàn)判斷當(dāng)前操作用戶管理角色的方法
- 01-10Extjs4如何處理后臺(tái)json數(shù)據(jù)中日期和時(shí)間
- 01-10C#中DataGridView常用操作實(shí)例小結(jié)
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10C#利用反射技術(shù)實(shí)現(xiàn)去掉按鈕選中時(shí)的邊框效果


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什