Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Perfecting the Prototype Censoring Words - Using String Equality

Can someone answer this

Ok I understand the equalsIgnoreCase() method. But what if you want to check if a String contains a particular String,ignoring the case

Example: String x = "I love Java"; now I want to check if the String x contains "Java" , ignoring the case

I know there is a method called contains , but no such thing as containsIgnoreCase So is there any solution

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I'm not aware of anything that will do a comparison with contains that ignores case. That being said, you can still accomplish this by making sure that everything is in the same case before you see if it "contains" that substring. I whipped up this code snippet for you. Feel free to run it in your workspace.

import java.io.Console;
public class Example {
  public static void main(String args[]) {

    Console console = System.console();    

    String language = "Java";
    String sentence = "I love java!";
    String lowerLanguage = language.toLowerCase();
    String lowerSentence = sentence.toLowerCase();

    if(lowerSentence.contains(lowerLanguage)) {
      console.printf("The sentence \"%s\" contains \"%s\" when ignoring case", sentence, language);
    }
  }
}

Hope this helps! :sparkles:

Thanks