Archive

Posts Tagged ‘string compare’

string.Equals compare with string.Compare Asp.Net

August 28th, 2009 aspgod No comments

When compare the string with not sure that the case of the string like (“aAa”, “aaa”, “AAA”). Normally I used the string.ToLower() to manage this problem but there’re many way to solved this. Look at my code below.

1
string.Compare(s1, s2, true)

This function return integer.
s1 > s2 then return 1
s1 < s2 then return -1
s1 = s2 then return 0
The 3rd pamerater is the "ignoreCase" then set it true to non-case sensitive

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
string s1 = "aAa";
if (s1.Equals("aaa")) // false because the case sensitive
{
    // do something
}
else if (s1 == "aaa") // false because the case sensitive
{
    // do something
}
else if (string.Compare(s1, "aaa", true) == 0) // true enable non-case sensitive
{
    // do something
}