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、測試代碼:
注:此測試針對(duì)的是擁有大量英文的情況下,并且指定的字符串為英文
每個(gè)方法測試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、測試結(jié)果:

3、總結(jié)
1、從測試結(jié)果(大量測試)中能明顯看出,當(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ù),返回值為是否包含子字符串:
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ù)大家的!
- 如何解決Mybatis--java.lang.IllegalArgumentException: Result Maps collection already contains value for X
- Python extract及contains方法代碼實(shí)例
- javascript中contains是否包含功能實(shí)現(xiàn)代碼(擴(kuò)展字符、數(shù)組、dom)
- Oracle 中Contains 函數(shù)的用法
- jQuery使用contains過濾器實(shí)現(xiàn)精確匹配方法詳解
- JavaScript中擴(kuò)展Array contains方法實(shí)例
- jQuery實(shí)現(xiàn)contains方法不區(qū)分大小寫的方法
- jQuery中:contains選擇器用法實(shí)例
- PowerShell Contains函數(shù)查找字符串實(shí)例
- Java contains用法示例
相關(guān)文章
asp.net core項(xiàng)目mvc權(quán)限控制:分配權(quán)限
學(xué)習(xí)的最好方法就是動(dòng)手去做,這里以開發(fā)一個(gè)普通的權(quán)限管理系統(tǒng)的方式來從零體驗(yàn)和學(xué)習(xí)Asp.net Core。項(xiàng)目的整體規(guī)劃大致如下2017-02-02
C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量二
這篇文章繼續(xù)介紹了C#數(shù)據(jù)類型和變量,是對(duì)上一篇文章的補(bǔ)充,希望對(duì)大家的學(xué)習(xí)有所幫助。2015-10-10
C#實(shí)現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法
這篇文章主要介紹了C#實(shí)現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法,結(jié)合實(shí)例形式簡單分析了C#字符數(shù)組轉(zhuǎn)字符串及字符串轉(zhuǎn)字符數(shù)組的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02
C#使用NPOI對(duì)Excel數(shù)據(jù)進(jìn)行導(dǎo)入導(dǎo)出
這篇文章介紹了C#使用NPOI對(duì)Excel數(shù)據(jù)進(jìn)行導(dǎo)入導(dǎo)出的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
C# 操作PostgreSQL 數(shù)據(jù)庫的示例代碼
本篇文章主要介紹了C# 操作PostgreSQL 數(shù)據(jù)庫的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11

