Starting with "Remove the space of the string"

Source: Internet
Author: User

1. Preface

The cause of the incident is this. I want to deal with thisProgramThe user can enter 11 digits + spaces in any form, and then I want to divide it into 434 formats.

The method is to remove all spaces, divide them into arrays according to the rules, and then output them.

So how to remove spaces, the general method is as follows:

A. String. Replace ()

B. String. Split (New char [] {''}, stringsplitoptions. removeemptyentries );

C. System. Text. RegEx () for regular expression replacement

D. Someone proposed to use stringbuilder, but we should never forget the critical value of stringbuilder. In the case of small data, using stringbuilder is not worth the candle.

2. High Performance Competition

Undoubtedly, regular expressions must be the slowest method.

First paste the testCode:

 Static void Main ( String [] ARGs ){ Stopwatch Sw =New  Stopwatch (); String Stemp = "11 1241 111 11" ; String Stest = String . Empty; For ( Int I = 0 ; I < 1000 ; I ++) {stest + = stemp;} Sw. Start (); For ( Int I =0 ; I < 1000 ; I ++) {stest. Replace ( "" , "" );} Sw. Stop (); Console . Write ( "Time consumed by using Replace :" ); Console . Writeline (SW. elapsedmilliseconds. tostring (); Sw. Reset (); Sw. Start (); For ( Int I = 0 ; I < 1000 ; I ++) {stest. Split ( New char [] { '' }, Stringsplitoptions . Removeemptyentries);} Sw. Stop (); Console . Write ( "Time consumed by split :" ); Console . Writeline (SW. elapsedmilliseconds. tostring ());}

The following is the test result:

 

As we expected, replace must be more efficient than split. The reason for speculation is actually very simple. Replace is used to replace characters, while split is used to split, this is a typical method for misuse.

Extended reading: ". net, have you forgotten? (8) -- from dynamic to feature misuse

3. Talk about string. Split ()

I cannot see the source code of string. Replace (), so here we will only talk about string. Split ().

The general code can be seen through reflector. Here, only the core code is pasted:

 Public String [] Split ( String [] Separator, Int Count, Stringsplitoptions Options ){ Bool Flag = Options = Stringsplitoptions . Removeemptyentries; Int [] Seplist = New int [ This . Length]; Int [] Lengthlist =New int [ This . Length]; Int Numreplaces = This . Makeseparatorlist (separator, Ref Seplist, Ref Lengthlist ); If (Numreplaces = 0 ) | (COUNT = 1 )){ Return New String [] { This };} If (FLAG ){Return this . Internalsplitomitemptyentries (seplist, lengthlist, numreplaces, count );} Return this . Internalsplitkeepemptyentries (seplist, lengthlist, numreplaces, count );}

First, let's take a look at the makeseparatorlist method. In this method, Microsoft traverses the delimiter array and string, and obtains the index of each Separator in the string through a two-layer for loop. The next step is to directly split the string based on the index.

4.AlgorithmAnalysis

From the code above, we can easily see that the time complexity of the algorithm is O (M * n). Do we have a better algorithm to implement it?

First, we need to see that the time complexity bottleneck of the algorithm lies in the makeseparatorlist method, so we can optimize this method and implement it as follows:

Private Static bool[] Makeseparatorlist (Char[] Separatorarray ){Bool[] Chararray =New bool[255];For(IntI =0; I <separatorarray. length; I ++) {chararray [separatorarray [I] =True;}ReturnChararray ;}

 

Then we can use the asc2 code of each character to determine whether the character is a separator when traversing the string.

5. Advantages and Disadvantages of Algorithms

Since the time complexity of such an algorithm is small, why does Microsoft not use it?

Time complexity does not determine the merits and demerits of algorithms. The so-called exponential, logarithm, or on-level algorithms are aimed at large data volumes. However, in this case, there is only one separator, but we need to declare a 255 bool array for this character, which is not worth the candle in space complexity.

So what is this algorithm suitable? This algorithm is suitable when there are multiple separators and long strings, so O (n) is much smaller than O (M * n.

6. Write it at the end

I haven't written anything for a long time, so I don't know what I'm writing.

Something is messy. forget it .......

Just alert yourself and everyone ,. net Framework provides us with a lot of class library method support, but before using a method, should we clarify whether this method is the best method, should we re-write a method, or ......

This is what programmers should do.

 

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.