11 Vote

JavaScript: String Comparison independent from Uppercase or Lowercase Writing

Question by Guest | Last update on 2024-07-22 | Created on 2013-12-10

From Java, I know the helpful function .equalsIgnoreCase() that can be used to compare two strings with ignoring the upper and lower case writing (case insensitive). In other words, "ABC" and "abc" are treated as equal using this function.

But how is it about JavaScript? It seems to me that such a function is not available there. So, how can I compare two Strings in JavaScript that ignores the case?

ReplyPositiveNegative
2Best Answer2 Votes

Unfortunately, this function is not implemented in JavaScript. So, first, you have to convert respectively rewrite both Strings to lower or upper case characters completely in order to carry out the comparison afterwards:

var s1 = "abc";
var s2 = "ABC";

if (s1.toUpperCase() === s2.toUpperCase()) {
   alert("Yes!");
}
Of course, this is a little bit more coding work and your code gets a little longer, but the result should be the same.
Last update on 2024-07-22 | Created on 2013-12-10

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.