欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

正文

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

  string.Contains是大小寫敏感的,如果要用該方法來判斷一個string是否包含某個關鍵字keyword,需要把這個string和這個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

 #那什么時候使用Contains方法,什么時候使用Index方法,哪個效率高?

1、測試代碼:

  注:此測試針對的是擁有大量英文的情況下,并且指定的字符串為英文

  每個方法測試1千萬次,輸出所用時間;

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("目標在開頭部分時:");
      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("目標在中部時:");
      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("目標在結尾時:");
      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、測試結果:

3、總結

  1、從測試結果(大量測試)中能明顯看出,當擁有大量英文的字符串中:

  *當不區(qū)分大小寫時,string.IndexOf方法的效率明顯高于string.Contains方法;

  *當區(qū)分大小寫時,string.Contains方法的效率明顯高于string.IndexOf方法;

  *如果判斷的是中文,沒有大小寫之分,還是string.Contains方法的效率高;

  2、綜合上述總結,定義了一個String擴展方法,該方法包含一個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

總結

以上所述是小編給大家介紹的C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

上一篇:C# TextBox數(shù)據(jù)綁定的方法

欄    目:C#教程

下一篇:C#簡單實現(xiàn)發(fā)送socket字符串

本文標題:C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5069.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有