00 Votes

JavaScript: String Comparison independent from Uppercase or Lowercase Writing

Question by Guest | 2013-12-10 at 14:49

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.

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

ReplyPositiveNegative
0Best Answer0 Votes

Unfortunately, this function is not implemented in JavaScript. So, first, you have to rewrite both Strings to lower or upper characters completely and do 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.
2013-12-10 at 16:45

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.