String Benchmarks in C# - Refactoring for Speed/Maintainability

后端 未结 10 1410
无人及你
无人及你 2021-02-09 21:29

I\'ve been tinkering with small functions on my own time, trying to find ways to refactor them (I recently read Martin Fowler\'s book Refactoring: Improving the Design of Ex

10条回答
  •  终归单人心
    2021-02-09 22:10

    I know what they say about RegEx, use it to solve a problem and now you have two problems, but I remain a fan, Just for grins, here is a RegEx version. RegEx, with a little initiation is easy to read, less code, and lets you easily snap in additional delimiters (as I did with the comma).

       s1 = MakeNiceString( "LookOut,Momma,There'sAWhiteBoatComingUpTheRiver" ) );
    
       private string MakeNiceString( string input )
       {
           StringBuilder sb = new StringBuilder( input );
           int Incrementer = 0;
           MatchCollection mc;
           const string SPACE = " ";
    
           mc = Regex.Matches( input, "[A-Z|,]" );
    
           foreach ( Match m in mc )
           {
               if ( m.Index > 0 )
               {
                   sb.Insert( m.Index + Incrementer, SPACE );
                   Incrementer++;
               }
           }
    
           return sb.ToString().TrimEnd();
       }
    

提交回复
热议问题