Let's say I have 2 strings. First string is x = "abc"
and the second one is y = "ABC"
. In C# when I write the following code:
if (x == y)
or
if (x.Equals(y))
the return value is true
. How can I check their upper and lower case?
Use the Compare static method on the string class to compare the two strings. Whether the comparison is case-insensitive is determined by the third parameter of one of its overloads. For example: string lowerCase = "abc"; string upperCase = "AbC"; int caseSensitiveResult = string.
You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
Comparing strings with special characters one needs to use Function ExactMatch to compare each character exactly. In the above code snippet, VarInputString is a Variable which is compared with another string " " (Space). Function ExactMatch returns True if the value of the Variable VarInputString equals to " ".
Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.
The return value is not true
but false
since .NET is case sensitive by default.
From String.Equals
:
This method performs an ordinal (case-sensitive and culture-insensitive) comparison.
For ==
the same is true since String.Equality
operator calls Equals
:
This operator is implemented using the Equals method, which means the comparands are tested for a combination of reference and value equality. This operator performs an ordinal comparison.
This will compare case insensitively:
bool equals = x.Equals(y , StringComparison.OrdinalIgnoreCase);
If you just want to know if a character is upper or lower case you can use these methods:
bool isUpperChar = Char.IsUpper("ABC"[0]); // yes
bool isLowerChar = Char.IsLower("ABC"[0]); // no
First, you should decide whether you compare strings in culture dependent
or independent way (e.g. in Russian Culture letters "E" and "Ё" often treats as being the same; Finnish tends to treat "V" and "W" as being the same etc.). Next you should choose whether use or not use case ("a" v. "A"). So there're 6 possible comparisons:
Ordinal (culture independent) comparisons:
// Ignore case comparison
Boolean equals = String.Equals(x, y, StringComparison.OrdinalIgnoreCase);
// Case comparison
Boolean equals = String.Equals(x, y, StringComparison.Ordinal);
Current culture comparisons:
// Current culture, ignore case comparison
Boolean equals = String.Equals(x, y, StringComparison.CurrentCulture);
// Current culture, case comparison
Boolean equals = String.Equals(x, y, StringComparison.CurrentCultureIgnoreCase);
Explicit culture comparisons:
CultureInfo culture = new CultureInfo("Ru-ru"); // <- Or whatever you want
// Explicit culture, ignore case comparison
Boolean equals = culture.CompareInfo.Compare(x, y, CompareOptions.IgnoreCase);
// Explicit culture, case comparison
Boolean equals = culture.CompareInfo.Compare(x, y, CompareOptions.None);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With