Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two strings and their upper and lower case signs

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?

like image 808
daidai Avatar asked Aug 22 '13 10:08

daidai


People also ask

How do you compare a string with uppercase and lowercase?

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.

Can you use == when comparing strings?

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.

How do you compare strings with special characters?

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 " ".

How do you compare 2 strings?

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.


2 Answers

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
like image 116
Tim Schmelter Avatar answered Oct 14 '22 08:10

Tim Schmelter


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);
like image 29
Dmitry Bychenko Avatar answered Oct 14 '22 07:10

Dmitry Bychenko