Recent comments

You are here

Java: Problems with containsIgnoreCase

This project came about because I wanted to use the containsIgnoreCase() function in Java. well it doesn't work!

Java can use equalsIgnoreCase however containsIgnoreCase doesnt work so this discribes a workaround

example: with equalsIgnoreCase() here we are checking to see if the string EQUALS the value we pass in this case "java"

String myString = ("Java is a great programming language");

if (myString.equalsIgnoreCase("java"))

{
YOUR COMMANDS HERE
}

This will not run your command because we are using the equalIgnoreCase() function and it contains it but doesn't EQUAL it

So now we try the containsIgnoreCase to see if it returns true this will search the string to see if it contains the word or phrase "java" in this case

String myString = ("Java is a great programming language");

if (myString.containsIgnoreCase("java"))

{
YOUR COMMANDS HERE
}

You might think that this would work AS DID I :/ nope it will find it if it contains it however it won't ignore the case LAME

Ok so what should we try next? took me a while but I figured out a workaround for this the work-around is to turn your string small BEFORE you pass it to the contains() function so we do like this

String myString = ("Java is a great programming language");

myString = myString.toLowerCase();

if (myString.contains("java"))

{
YOUR COMMANDS HERE
}

Here we set the string after it is set we switch it to lower case then all we need to do is search for the string in lowercase. Now it doesn't matter if the string had uppercase letters at all your commands will be executed as expected just make sure the word or phrase you are searching for is all in lower case since the whole string you are searching inside of has been converted to lowercase

Hope this helps someone as I havn't seen many answers to the containsIgnoreCase problem feel free to comment below and I will write back to help you fix any errors you may encounter. Also feel free to hit the "Share this on Faceboo" button on the top of this page :)

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer