Compare 2 Strings of Diff Length

I am looking for the fastest comparison which will start from the left of one string, and return true if ALL the characters of the first string match the first x characters of the 2nd string where x is the length of the first string.

For example:

String s1 = "YI" ;
String s2 = "YIPES" ;
String s3 = "Y" ;

s1 = s2 would return true
s1 = s3 would return false

This would be part of a larger loop, so I am looking for the fastest comparision method. Regex maybe

Many thaks
Mike Thomas











Answer this question

Compare 2 Strings of Diff Length

  • papadi

    s2.StartsWith(s1) returns true. s3.StartsWith(s1) returns false. You can also specify case-sensitivity and culture information.

  • Kamel Balquis

    Thanks very much CommonGenius.

    Mike Thomas

  • Compare 2 Strings of Diff Length