Several Modes and efficiency (string. Contains, String. indexof, RegEx. Match)

Source: Internet
Author: User

Generally, we can determine whether a value exists in a string. contains, in fact, there are many methods to determine the existence of a value in a string, the most common is the aforementioned string. contains, which are commonly used, are also string. indexof and RegEx. match. Let's talk about Code directly. In general, the implementation of functions is the most important. The author's words are only effective for those who are interested.

 

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 | 000 |"; // ---------------------------------------------- // string. contains method if (str2.contains (str0) console. WRI Teline ("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 method int val1 = str2.indexof (str0); // returns-1 Console if no value exists. writeline ("string. indexof (no exists)-> "+ val1); int val2 = str2.indexof (str1); // s is returned Location (> = 0) in str2 where tr1 is located console. writeline ("string. indexof (exists)-> "+ val2); // -------------------------------------------- // Regular Expression matching method 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 ("re Gex. Match (exists)-> false "); console. readkey ();/** what is the efficiency if all three methods process large amounts of data? * Description of the following six groups of data: */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 method // 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 method // No exists lasttime = datetime. now; For (INT I = 0; I <loopcount; I ++) if (str2.indexof (str0) >=0) {}; // If no result is returned as mentioned above, returned results A non-negative integer. Why not use =-1 instead of> = 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 Method // 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 ();}}}

Input result:

String. Contains-> false
String. Contains-> true
String. indexof (no exists)->-1
String. indexof (exists)-> 12
RegEx. Match (no exists)-> false
RegEx. Match (exists)-> true

Loop 1 >>>>>>>
No exists >>>
TS strconnoexists = 796
TS strindnoexists = 687
Tsstrregnoexists = 171
Exists >>>
TS strconexists = 484
TS strindexists = 234
Tsstrregexists = 796

Loop 2 >>>>>>>
No exists >>>
Tsstrconnoexists = 46
TS strindnoexists = 671
Tsstrregnoexists = 234
Exists >>>
TS strconexists = 546
TS strindexists = 437
Tsstrregexists = 734

Loop 3 >>>>>>>
No exists >>>
Tsstrconnoexists = 62
TS strindnoexists = 875
Tsstrregnoexists = 171
Exists >>>
TS strconexists = 609
TS strindexists = 562
Tsstrregexists = 781

Loop 4 >>>>>>>
No exists >>>
Tsstrconnoexists = 78
TS strindnoexists = 921
Tsstrregnoexists = 218
Exists >>>
TS strconexists = 609
TS strindexists = 640
Tsstrregexists = 828

Loop 5 >>>>>>>
No exists >>>
TS strconnoexists = 156
TS strindnoexists = 268
Tsstrregnoexists = 265
Exists >>>
TS strconexists = 609
TS strindexists = 578
Tsstrregexists = 890

Loop 6 >>>>>>>
No exists >>>
TS strconnoexists = 109
Tsstrindnoexists = 46
Tsstrregnoexists = 546
Exists >>>
TS strconexists = 625
TS strindexists = 609
Tsstrregexists = 953

 

In the test results, it is not difficult to find that if strb is not included in stra, it is better to use stra. Contains (strb). If strb is included in stra, it is better to use stra. indexof (strb. (RegEx. Match does not seem to have any advantage in this method, and it is more suitable for fuzzy match)

Use string. contains or string. indexof to view the situation.

I have read the code implemented by many methods in string (Microsoft, not others). String. Contains is a method based on string. indexof. It is called when string. Contains is used.

String. indexof, according to the principle, use string. the efficiency of indexof is higher than that of string. contains, but this test result surprised me. It should be the result of this unsatisfactory test caused by the judgment statement I used in the above Code. According to my personal wishes, you still want to use string. indexof.

 

In fact, a small change may not affect much at present, but its advantage is obvious over time. To quickly become stronger than others, you only need to do a little more (1‰) every day)

One year later: (1 + 0.001) 365 = 1.44 times

Ten years later (1 + 0.001) 3650 = 38.4 times

 

This is a kind of advantageous computing, it is a kind of ability computing, it is a kind of salary calculation ,..., no matter what the computing is, remember to make a little progress every day. As time goes on, you will change. You should eat, drink, play, and sleep... you just need to use the time you spend everyday dreaming and having no purpose to do meaningless things, and you are making progress.

Several Modes and efficiency (string. Contains, String. indexof, RegEx. Match)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.