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

String method "contains".

Hi,

I just finished the "Java basics" and I need a bit of help. There is this extra credit related to String method called "contains". I've searched the internet, but since my english isn't the best i still don't get it. Could someone help me and explain me that in a few simple words / simple code? Cheers!

2 Answers

Contains method is used to check if a string contains a sequence of characters, this could be one letter or few, one word or few words. lets say you have the following string

String example = "My name is Jan;
String firstName = "Jan";

And you want to check if your name is in the string, you would do this:

 System.out.println(example.contains(firstName));

This would return true.

 System.out.println(example.contains("Treehouse"));

would return false, since the string example does not contain the word Treehouse.

Thank You alot!