There are a number of ways to compare strings. Are there performance gains by doing one way over another?
I\'ve always opted to compare strings like so:
To best way to compare string
's in C# is to use the a.Equals(b)
where a and b are strings. This is the best way to compare string because it compares the values of the objects a and b, and does not depent on the reference of the objects.
If you're going to use "==
" symbol, the result will be equal if both objects have the same reference but you will have a problem when they have different references and have the same value.
The compareTo
method is best way to use if your testing whether the other string is preceding, following or appearing in the same position of the other string wherein it will return negative value , positive value or zero value respectively. It will return also positive value if the parameter is null