Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 533597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:31:29+00:00 2026-05-13T09:31:29+00:00

I need to do alot of high-performance case-insensitive string comparisons and realized that my

  • 0

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

So I digged around a little and this way seems preferable:

String.Compare(txt1,txt2, StringComparison.OrdinalIgnoreCase)

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

public static int Compare
(
    string strA,
    int indexA,
    string strB,
    int indexB,
    int length,
    StringComparison comparisonType
) 

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?

Here’s my current proposal:

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) > 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);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:31:30+00:00Added an answer on May 13, 2026 at 9:31 am

    Something like this should do it:

    public static int TrimCompareIgnoreCase(string a, string b) {
       int indexA = 0;
       int indexB = 0;
       while (indexA < a.Length && Char.IsWhiteSpace(a[indexA])) indexA++;
       while (indexB < b.Length && Char.IsWhiteSpace(b[indexB])) indexB++;
       int lenA = a.Length - indexA;
       int lenB = b.Length - indexB;
       while (lenA > 0 && Char.IsWhiteSpace(a[indexA + lenA - 1])) lenA--;
       while (lenB > 0 && Char.IsWhiteSpace(b[indexB + lenB - 1])) lenB--;
       if (lenA == 0 && lenB == 0) return 0;
       if (lenA == 0) return 1;
       if (lenB == 0) return -1;
       int result = String.Compare(a, indexA, b, indexB, Math.Min(lenA, lenB), true);
       if (result == 0) {
          if (lenA < lenB) result--;
          if (lenA > lenB) result++;
       }
       return result;
    }
    

    Example:

    string a = "  asdf ";
    string b = " ASDF \t   ";
    
    Console.WriteLine(TrimCompareIgnoreCase(a, b));
    

    Output:

    0
    

    You should profile it against a simple Trim and Compare with some real data, to see if there really is any difference for what you are going to use it for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a system that requires high file I/O performance (with C#). Basically,
My question is about a high performance way to implement auto-saving in an ASP.NET
I need fast and simple way to encrypt/decrypt a lot of String data. I
Hi, I have a Sevice with a alot of operations, I need to wrapp
TickZoom is a very high performance app which uses it's own parallelization library and
a quick, simple question from me about for-loops. Situation I'm currently writing some high-performance
I've inherited a lot of web projects that experienced high developer turn over rates.
I desperately need help with a query that's been causing a lot of grief
In this case, I'm working on a project that involves a database of library
we got high-load java application which works in clustered mode. I need to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.