欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問(wèn)題

 更新時(shí)間:2018年10月14日 11:51:15   作者:willingtolove  
這篇文章主要介紹了C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問(wèn)題 ,文中給大家列舉通過(guò)兩種方法來(lái)判斷,需要的朋友可以參考下

正文

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

  string.Contains是大小寫(xiě)敏感的,如果要用該方法來(lái)判斷一個(gè)string是否包含某個(gè)關(guān)鍵字keyword,需要把這個(gè)string和這個(gè)keyword都轉(zhuǎn)成小寫(xiě)或大寫(xiě)再調(diào)用Contains方法;

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

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

  使用string.Index方法,然后通過(guò)StringComparison.OrdinalIgnoreCase指定查找過(guò)程忽略大小寫(xiě);

 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千萬(wàn)次,輸出所用時(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)在開(kāi)頭部分時(shí):");
      Console.WriteLine("不區(qū)分大小寫(xiě):");
      TestContains(source, target,true);
      TestIndexOf(source, target,true);
      Console.WriteLine("區(qū)分大小寫(xiě):");
      target = "aaa";
      TestContains(source, target,false);
      TestIndexOf(source, target,false);
      Console.WriteLine();
      Console.WriteLine("目標(biāo)在中部時(shí):");
      Console.WriteLine("不區(qū)分大小寫(xiě):");
      target = "HHH";
      TestContains(source, target, true);
      TestIndexOf(source, target, true);
      Console.WriteLine("區(qū)分大小寫(xiě):");
      target = "hhh";
      TestContains(source, target, false);
      TestIndexOf(source, target, false);
      Console.WriteLine();
      Console.WriteLine("目標(biāo)在結(jié)尾時(shí):");
      Console.WriteLine("不區(qū)分大小寫(xiě):");
      target = "QQQ";
      TestContains(source, target,true);
      TestIndexOf(source, target,true);
      Console.WriteLine("區(qū)分大小寫(xiě):");
      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ū)分大小寫(xiě)時(shí),string.IndexOf方法的效率明顯高于string.Contains方法;

  *當(dāng)區(qū)分大小寫(xiě)時(shí),string.Contains方法的效率明顯高于string.IndexOf方法;

  *如果判斷的是中文,沒(méi)有大小寫(xiě)之分,還是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方法效率問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

最新評(píng)論