国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

字符串中判斷存在的幾種模式和效率(string.contains、string.IndexOf、Regex.Match)

2019-11-17 02:57:28
字體:
來源:轉載
供稿:網友

字符串中判斷存在的幾種模式和效率(string.contains、string.IndexOf、Regex.Match)

  通常情況下,我們判斷一個字符串中是否存在某值常常會用string.contains,其實判斷一個字符串中存在某值的方法有很多種,最常用的就是前述所說的string.contains,相對來說比較常用的還有string.IndexOf和Regex.Match。直接上代碼,后面在說些什么吧,通常情況下功能的實現最重要,作者的話,只對有心者有效。


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExPRessions;namespace ExistsInString{    class Program    {        static void Main(string[] args)        {            string str0 = "|456|";            string str1 = "|444|";            string str2 = "|111|222|333|444|555|666|777|888|999|000|";            //------------------------------------------            //String.Contains方法            if (str2.Contains(str0))                Console.WriteLine("String.Contains->true");            else                Console.WriteLine("String.Contains->false");            if (str2.Contains(str1))                Console.WriteLine("String.Contains->true");            else                Console.WriteLine("String.Contains->false");            //------------------------------------------            //String.IndexOf方法            int val1 = str2.IndexOf(str0);//不存在返回-1            Console.WriteLine("String.IndexOf(no exists)->" + val1);            int val2 = str2.IndexOf(str1);//存在返回str1首字符所在str2中的位置(>=0)            Console.WriteLine("String.IndexOf(exists)->" + val2);            //------------------------------------------            //正則匹配方法            if (Regex.Match(str2, "[|]456[|]").Success)                Console.WriteLine("Regex.Match(no exists)->true");            else                Console.WriteLine("Regex.Match(no exists)->false");            if (Regex.Match(str2, "[|]444[|]").Success)                Console.WriteLine("Regex.Match(exists)->true");            else                Console.WriteLine("Regex.Match(exists)->false");            Console.ReadKey();            /*             *如果上述三種方式都處理大量數據,效率如何呢?             *以下循環六組數據說明              */            int loopCount = (int)10e6;            DateTime lasttime = DateTime.Now;            DateTime nowtime = DateTime.Now;            for (int loop = 1; loop < 7; loop++)            {                Console.WriteLine("/r/nloop " + loop + " >>>>>>>");                //------------------------------------------                //String.Contains方法                //no exists                lasttime = DateTime.Now;                for (int i = 0; i < loopCount; i++)                    if (str2.Contains(str0)) { };                nowtime = DateTime.Now;                TimeSpan tsStrConNoExists = nowtime - lasttime;                //exists                lasttime = DateTime.Now;                for (int i = 0; i < loopCount; i++)                    if (str2.Contains(str1)) { };                nowtime = DateTime.Now;                TimeSpan tsStrConExists = nowtime - lasttime;                //------------------------------------------                //String.IndexOf方法                //no exists                lasttime = DateTime.Now;                for (int i = 0; i < loopCount; i++)                    if (str2.IndexOf(str0) >= 0) { };//上述已經提到不存在返回-1,存在返回一個非負整數,這里為什么不用 == -1 ,而是用了 >= 0 ,這是一個值得深思的問題?                nowtime = DateTime.Now;                TimeSpan tsStrIndNoExists = nowtime - lasttime;                //exists                lasttime = DateTime.Now;                for (int i = 0; i < loopCount; i++)                    if (str2.IndexOf(str1) >= 0) { };                nowtime = DateTime.Now;                TimeSpan tsStrIndExists = nowtime - lasttime;                //------------------------------------------                //Regex.Match方法                //no exists                Regex Reg0 = new Regex("[|]456[|]");                lasttime = DateTime.Now;                for (int i = 0; i < loopCount; i++)                    if (Reg0.Match(str2).Success) { };                nowtime = DateTime.Now;                TimeSpan tsStrRegNoExists = nowtime - lasttime;                //exists                Regex Reg1 = new Regex("[|]444[|]");                lasttime = DateTime.Now;                for (int i = 0; i < loopCount; i++)                    if (Reg1.Match(str2).Success) { };                nowtime = DateTime.Now;                TimeSpan tsStrRegExists = nowtime - lasttime;                Console.WriteLine("no exists >>>");                Console.WriteLine("tsStrConNoExists = " + tsStrConNoExists.Milliseconds);                Console.WriteLine("tsStrIndNoExists = " + tsStrIndNoExists.Milliseconds);                Console.WriteLine("tsStrRegNoExists = " + tsStrRegNoExists.Milliseconds);                Console.WriteLine("exists >>>");                Console.WriteLine("tsStrConExists = " + tsStrConExists.Milliseconds);                Console.WriteLine("tsStrIndExists = " + tsStrIndExists.Milliseconds);                Console.WriteLine("tsStrRegExists = " + tsStrRegExists.Milliseconds);            }            Console.ReadKey();        }    }}

輸入結果:

String.Contains->falseString.Contains->trueString.IndexOf(no exists)->-1String.IndexOf(exists)->12Regex.Match(no exists)->falseRegex.Match(exists)->true

loop 1 >>>>>>>no exists >>>tsStrConNoExists = 796tsStrIndNoExists = 687tsStrRegNoExists = 171exists >>>tsStrConExists = 484tsStrIndExists = 234tsStrRegExists = 796

loop 2 >>>>>>>no exists >>>tsStrConNoExists = 46tsStrIndNoExists = 671tsStrRegNoExists = 234exists >>>tsStrConExists = 546tsStrIndExists = 437tsStrRegExists = 734

loop 3 >>>>>>>no exists >>>tsStrConNoExists = 62tsStrIndNoExists = 875tsStrRegNoExists = 171exists >>>tsStrConExists = 609tsStrIndExists = 562tsStrRegExists = 781

loop 4 >>>>>>>no exists >>>tsStrConNoExists = 78tsStrIndNoExists = 921tsStrRegNoExists = 218exists >>>tsStrConExists = 609tsStrIndExists = 640tsStrRegExists = 828

loop 5 >>>>>>>no exists >>>tsStrConNoExists = 156tsStrIndNoExists = 268tsStrRegNoExists = 265exists >>>tsStrConExists = 609tsStrIndExists = 578tsStrRegExists = 890

loop 6 >>>>>>>no exists >>>tsStrConNoExists = 109tsStrIndNoExists = 46tsStrRegNoExists = 546exists >>>tsStrConExists = 625tsStrIndExists = 609tsStrRegExists = 953


測試結果中不難發現,如果strA中不包括strB,使用strA.Contains(strB)更優;反之,如果strA中包括strB,使用strA.IndexOf(strB)更優。(Regex.Match在此方法中貌似沒有體現出任何優勢,它更適用于模糊匹配)

具體要使用string.Contains,或是string.IndexOf要看形勢。

之前有看過string下很多方法實現的代碼(微軟的,非他人),string.Contains是基于string.IndexOf上的一個方法,使用string.Contains的時候,會調用

string.IndexOf,按原理,使用string.IndexOf的效率是要高于string.Contains的,但是這個測試結果讓我大跌眼鏡,應該是我在上述代碼中使用的判斷語句造成的這種非理想的測試結果,按照個人的意愿,還是希望多使用string.IndexOf。


其實一次微小的改變在當前可能影響不了什么,但是在日積月累中,它的優勢就顯而易見了。想要快速變得比他人更強,不需要多么費勁,只需要每天多做一點點(千分之一)

一年之后:(1 + 0.001)365 = 1.44倍

十年之后(1 + 0.001)3650= 38.4倍


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 美姑县| 昌江| 普安县| 栾川县| 蒙山县| 新余市| 金寨县| 土默特右旗| 肥东县| 高州市| 昌都县| 巍山| 承德市| 七台河市| 柳江县| 彝良县| 琼海市| 稷山县| 镇雄县| 天气| 德阳市| 临安市| 邻水| 莱芜市| 杭州市| 平和县| 灵石县| 始兴县| 贵阳市| 曲水县| 拉萨市| 塘沽区| 安宁市| 揭西县| 阳信县| 永安市| 湄潭县| 岗巴县| 河曲县| 柳林县| 常德市|