Beginner Beginner - 1 month ago 6
Java Question

Check if a specific string contains any lower/uppercase

Is there a way to check if a specific string contains any lower/upper case ?

E.G :

String myStr = "test";
if (myStr.contains("test") { // I would like this condition to
check the spell of "test", weather it is
written like "Test" or "teSt" etc...
//Do stuff
}


With this syntax, it works only for the exact same string. How could I make my condition for anyform of "test", like : "Test", "tEst", "tesT" etc... ?

Answer

You can use equalsIgnoreCase() method. This method compares two String with ignoring the case.

String a = "Test";

if("test".equalsIgnoreCase(a)) //returns true
{ 
  //do stuff
}