Comparing Two Strings, Ignoring Case in C#

Comparing two strings, ignoring case in C#

The first one is the correct one, and IMHO the more efficient one, since the second 'solution' instantiates a new string instance.

Best way to compare two strings ignoring case

If you have a look at the corresponding reference sources

https://referencesource.microsoft.com/#mscorlib/system/string.cs,bda3b2c94b5251ce

    public static int Compare(String strA, String strB, bool ignoreCase)
{
if (ignoreCase) {
return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.IgnoreCase);
}
return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.None);
}

https://referencesource.microsoft.com/#mscorlib/system/string.cs,0be9474bc8e160b6

    public static int Compare(String strA, String strB, StringComparison comparisonType) 
{
...
// Agrument validation, reference equality, null test

switch (comparisonType) {
...
case StringComparison.CurrentCultureIgnoreCase:
return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.IgnoreCase);

https://referencesource.microsoft.com/#mscorlib/system/string.cs,d47c1f57ad1e1e6e

    public static bool Equals(String a, String b, StringComparison comparisonType) {
...
// Agrument validation, reference equality, null test

switch (comparisonType) {
...
case StringComparison.CurrentCultureIgnoreCase:
return (CultureInfo.CurrentCulture.CompareInfo.Compare(a, b, CompareOptions.None) == 0);

you'll find these three methods being equal one another. As for other ways, Regex.IsMatch is definitely an overshoot (all you have to do is to compare strings); ToLower() can be tricky when dealing with culture specific letters, see

https://en.wikipedia.org/wiki/Dotted_and_dotless_I

that's why a better design is to declare your intends clearly (= I want to compare strings) then mask them (and let the system decieve you)

Ignore case and compare in C#

use this:

var result = String.Compare("AA", "aa", StringComparison.OrdinalIgnoreCase);

String.Compare Method (String, String, Boolean)

Is there a C# case insensitive equals operator?

Try this:

string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);

C# comparing strings ignoring both: whitespaces, CR etc and cases

You can use both CompareOptions, IgnoreCase and IgnoreSymbols if you add |.

Example:

string s1 = " A ";
string s2 = "a";
string.Compare(s1, s2, CultureInfo.CurrentCulture,
CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols);

Be aware IgnoreSymbols doesn't only ignore Whitespaces but others as well. See Microsoft Docs for more detail on the CompareOptions.

Case Insensitive comparison in C#

Try this:

string.Equals("this will return true", "ThIs WiLL ReTurN TRue", StringComparison.CurrentCultureIgnoreCase)

Or, for contains:

if (string1.IndexOf(string2, StringComparison.CurrentCultureIgnoreCase) >= 0)

C# compare string ignoreCase

Using the Assert.AreEqual with the ignoreCase parameter is better because it doesn't require the creation of a new string (and, as pointed out by @dtb, you could work following the rules of a specific culture info)

Assert.AreEqual(user1.UserName, user2.UserName, true, CultureInfo.CurrentCulture);

C# string comparison ignoring spaces, carriage return or line breaks

Remove all the characters you don't want and then use the ToLower() method to ignore case.

edit: While the above works, it's better to use StringComparison.OrdinalIgnoreCase. Just pass it as the second argument to the Equals method.



Related Topics



Leave a reply



Submit