Monday, March 12, 2012

c# compare two strings

To compare two strings using c# use String.Compare Method.

The signature for the method is 
public static int Compare(string strA,string strB,bool ignoreCase)

Example for a case sensitive string comparison:

string a = "hi";
string b = "HI";

bool isMatching; 
//false below means do not ignore case
if (string.Compare(a, b, false)== 0) 
    isMatching = true;
    isMatching = false;
Console.WriteLine(isMatching.ToString());//Displays False

No comments:

Post a Comment

Comments will appear once they have been approved by the moderator