by Edward
25 September 2009 05:55
Comparing string values can be very frustrating as you would sometimes need to check for NULL values, the correct case, and empty strings. Because "string" can be NULL, one should always try avoiding the equality symbol. You should rather use the static String.Compare method. This method compare strings ignoring case(which is good for comparing user input), and compare strings using a specific culture. It can also handle NULL string references, which makes it the first choise method to compare strings.
For Example:
string string1 = "DasCode.Net";
string string2 = "DasCode";
if (String.Compare(string1, string2) == 0)
{
// if true
Response.Write("true");
}
else
{
// if false
Response.Write("false");
}
String.Compare also have several overload methods, which you can read more about here.