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

  • Home
  • SEARCH
  • 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 7057887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:02:50+00:00 2026-05-28T04:02:50+00:00

I am using string comparisons to test URL paths using StringComparison.OrdinalIgnoreCase . MSDN gives

  • 0

I am using string comparisons to test URL paths using StringComparison.OrdinalIgnoreCase.

MSDN gives the following string comparison advice HERE, but does not clarify WHY:

MSDN Example (half-way down the above page):

public static bool IsFileURI(string path) 
{
   path.StartsWith("FILE:", StringComparison.OrdinalIgnoreCase);
   return true;
}

MSDN Advice:

“However, the preceding example uses the String.StartsWith(String, StringComparison) method to test for equality. Because the purpose of the comparison is to test for equality instead of ordering the strings, a better alternative is to call the Equals method, as shown in the following example.”

public static bool IsFileURI(string path)
{
   if (path.Length < 5) return false;

   return String.Equals(path.Substring(0, 5), "FILE:", 
                    StringComparison.OrdinalIgnoreCase);
}

QUESTION: Why does MSDN suggest the second example is better?

Discussion points:

  1. Clearly the return true; in the first example is a bug and should be return path.StartsWith(...);. We can safely ignore this as a bug as the VB code is correct.

  2. Creation of a substring prior to comparing for equality would appear to only use another memory resource than just calling String.StartsWith().

  3. The Length < 5 test is a nice short-circuit, however it could be used with the prior code just the same.

  4. The second example could be construed as clearer code, but I am concerned with performance. The creation of the substring seems unnecessary.

  • 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-28T04:02:50+00:00Added an answer on May 28, 2026 at 4:02 am

    Looking at the StartsWith method using dotPeek, it eventually calls an internal comparison function that compares the entire string, and returns a boolean result based on the return value of that comparison:

    return TextInfo.CompareOrdinalIgnoreCaseEx(this, 0, value, 0, value.Length, value.Length) == 0;
    

    String.Equals calls:

    return TextInfo.CompareOrdinalIgnoreCase(this, value) == 0;
    

    CompareOrdinalIgnoreCase calls a private method, which dotPeek doesn’t show, but my hunch is that the overload called by StartsWith traverses the entire string while the overload called by Equals stops as soon as equality can be determined.

    If performance is a concern, try measuring both with values that will be typical for your application.


    Out of curiousity, I tried measuring the two, and it does seem that Equals is noticeably faster. When I run the code below using a release build, Equals is nearly twice as fast as StartsWith:

    using System;
    using System.Diagnostics;
    
    namespace ConsoleApplication1
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                var url = "http://stackoverflow.com/questions/8867710/is-string-equalsstring1-substring0-x-string2-better-than-string1-startswit";
                var count = 10000000;
                var http = false;
    
                Stopwatch sw = Stopwatch.StartNew();
    
                for (int i = 0; i < count; i++)
                {
                    http = url.StartsWith("http:", StringComparison.OrdinalIgnoreCase);
                }
    
                sw.Stop();
    
                Console.WriteLine("StartsWith: {0} ms", sw.ElapsedMilliseconds);
    
                sw.Restart();
    
                for (int i = 0; i < count; i++)
                {
                    http = string.Equals(url.Substring(0, 5), "http:", StringComparison.OrdinalIgnoreCase);
                }
    
                sw.Stop();
    
                Console.WriteLine("Equals: {0} ms", sw.ElapsedMilliseconds);
    
                Console.ReadLine();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using string template to render some content, but the content may be
I am using std::string 's find() method to test if a string is a
Using String.Format how can i ensure all numbers have commas after every 3 digits
How can brackets be escaped in using string.Format ? For example: String val =
I've got an application that's using string.compare(string,string) to sort some values. The thing I
A recent question came up about using String.Format(). Part of my answer included a
Is there some way I can define String[int] to avoid using String.CharAt(int) ?
I am trying to read contents of a file using string tokenizer and store
Are there any codes that allow for numerical formatting of data when using string.format?
In Python, the where and when of using string concatenation versus string substitution eludes

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.