Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should StartsWith only be used for ordering?

Tags:

string

c#

I was reading Microsoft's Best Practices for Using Strings in the .NET Framework.

It gives the following example as an introduction to StringComparison.OrdinalIgnoreCase:

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

So far, so good. But it then goes on to say this:

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);
}   

I'm struggling to see why the second version is better. I could understand switching from CompareTo (comparison) to Equals (equality), but isn't StartsWith also an equality test? Am I missing something or is this a documentation bug?

like image 715
Matthew Strawbridge Avatar asked Oct 01 '12 20:10

Matthew Strawbridge


People also ask

What does startsWith function do?

STARTSWITH is a string manipulation function that manipulates all string data types (BIT, BLOB, and CHARACTER), and returns a Boolean value to indicate whether one string begins with another.

Is startsWith in C# case sensitive?

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture.

What does startsWith return in C#?

In C#, StartsWith() is a string method. This method is used to check whether the beginning of the current string instance matches with a specified string or not. If it matches then it returns the string otherwise false.

How to check starting string?

To check if a string starts with a substring, call the indexOf() method on the string, passing it the substring as a parameter. If the indexOf method returns 0 , then the string starts with the substring, otherwise it doesn't.


1 Answers

Not a real answer, but StartsWith() is an equality test and I think it's a documentation bug, but I was curious to know the performances, so I did a banchmark using the following code:

class Program {
    static void Main( string[ ] args ) {
        Stopwatch sw = Stopwatch.StartNew( );
        for ( int i = 0; i < 1000000000; i++ ) //1 billion times
            IsFileURI1( "File:\\ThisIsATest" );
        sw.Stop( );
        Console.WriteLine( "String.StartsWith(): " + sw.ElapsedMilliseconds.ToString( ) );

        sw.Restart( );
        for ( int i = 0; i < 1000000000; i++ ) //1 billion times
            IsFileURI2( "File:\\ThisIsATest" );
        sw.Stop( );
        Console.WriteLine( "String.Equals(): " + sw.ElapsedMilliseconds.ToString( ) );
    }

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

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

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

The results are (milliseconds):

String.StartsWith(): 90102 
String.Equals(): 73113

So in terms of performance the second solution is better and is about the 20% faster.

like image 108
Omar Avatar answered Sep 19 '22 17:09

Omar