Fastest way to trim a string and convert it to lower case

后端 未结 5 1287
情歌与酒
情歌与酒 2021-01-17 09:42

I\'ve written a class for processing strings and I have the following problem: the string passed in can come with spaces at the beginning and at the end of the string.

相关标签:
5条回答
  • 2021-01-17 10:18

    Here's my attempt. But before I would check this in, I would ask two very important questions.

    1. Are sequential "String.Trim" and "String.ToLower" calls really impacting the performance of my app? Would anyone notice if this algorithm was twice as slow or twice as fast? The only way to know is to measure the performance of my code and compare against pre-set performance goals. Otherwise, micro-optimizations will generate micro-performance gains.

    2. Just because I wrote an implementation that appears faster, doesn't mean that it really is. The compiler and run-time may have optimizations around common operations that I don't know about. I should compare the running time of my code to what already exists.

      static public string TrimAndLower(string str)
      {
      
          if (str == null)
          {
              return null;
          }
      
          int i = 0;
          int j = str.Length - 1;
          StringBuilder sb;
      
          while (i < str.Length)
          {
              if (Char.IsWhiteSpace(str[i])) // or say "if (str[i] == ' ')" if you only care about spaces
              {
                  i++;
              }
              else
              {
                  break;
              }
          }
      
          while (j > i)
          {
              if (Char.IsWhiteSpace(str[j])) // or say "if (str[j] == ' ')" if you only care about spaces
              {
                  j--;
              }
              else
              {
                  break;
              }
          }
      
          if (i > j)
          {
              return "";
          }
      
          sb = new StringBuilder(j - i + 1);
      
          while (i <= j)
          {
              // I was originally check for IsUpper before calling ToLower, probably not needed
              sb.Append(Char.ToLower(str[i]));
              i++;
          }
      
          return sb.ToString();
      }
      
    0 讨论(0)
  • 2021-01-17 10:29

    So first of all, trim first and replace second, so you have to iterate over a smaller string with your ToLower()

    other than that, i think your best algorithm would look like this:

    • Iterate over the string once, and check
      • whether there's any upper case characters
      • whether there's whitespace in beginning and end (and count how many chars you're talking about)
    • if none of the above, return the original string
    • if upper case but no whitespace: do ToLower and return
    • if whitespace:
      • allocate a new string with the right size (original length - number of white chars)
      • fill it in while doing the ToLower
    0 讨论(0)
  • 2021-01-17 10:34

    Cyberdrew has the right idea. With string being immutable, you'll be allocating memory during both of those calls regardless. One thing I'd like to suggest, if you're going to call string.Trim().ToLower() in many locations in your code, is to simplify your calls with extension methods. For example:

    public static class MyExtensions
    {
        public static string TrimAndLower(this String str)
        {
            return str.Trim().ToLower();
        }
    }   
    
    0 讨论(0)
  • 2021-01-17 10:38

    If the strings use only ASCII characters, you can look at the C# ToLower Optimization. You could also try a lookup table if you know the character set ahead of time

    0 讨论(0)
  • 2021-01-17 10:40

    Try method chaining.

    Ex:

    var s = " YoUr StRiNg".Trim().ToLower();
    
    0 讨论(0)
提交回复
热议问题