title: String.comparison performance (with trim)
body: <p>I need to do alot of high-performance case-insensitive string comparisons and realized that my way of doing it   .ToLower().Trim() was really stupid due do all the new strings being allocated</p>
  
  <p>So I digged around a little and this way seems preferable: </p>
  
  <pre><code>String.Compare(txt1,txt2, StringComparison.OrdinalIgnoreCase)
  </code></pre>
  
  <p>The only problem here is that I want to ignore leading or trailing spaces, ie Trim() but if I use Trim I have the same problem with string allocations. I guess I could check each string and see if it StartsWith(" ") or EndsWith(" ") and only then Trim. Either that or figure out the index,length for each string and pass to string.Compare override </p>
  
  <pre><code>public static int Compare
  (
      string strA,
      int indexA,
      string strB,
      int indexB,
      int length,
      StringComparison comparisonType
  ) 
  </code></pre>
  
  <p>but that seems rather messy and I probably have to to use some integers if I dont make a really big if-else statement for every combination of trailing and leading blanks on both strings... so any ideas of an elegant solution?</p>
  
  <p>Here's my current proposal:</p>
  
  <pre><code>public bool IsEqual(string a, string b)
      {
          return (string.Compare(a, b, StringComparison.OrdinalIgnoreCase) == 0);
      }
  
      public bool IsTrimEqual(string a, string b)
      {
          if (Math.Abs(a.Length- b.Length) &gt; 2 ) // if length differs by more than 2, cant be equal
          {
              return  false;
          }
          else if (IsEqual(a,b))
          {
              return true;
          }
          else 
          {
              return (string.Compare(a.Trim(), b.Trim(), StringComparison.OrdinalIgnoreCase) == 0);
          }
      }
  </code></pre>