How do i find a string inside a string?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How do i find a string inside a string??

For eg: I have: String str = "I love you" How do I check that the word love exists in the string ?

11th Aug 2018, 1:04 PM
Rishabh
Rishabh - avatar
3 Answers
+ 8
Use contains() method if (str.contains("love"){ System.out.print(true); }
11th Aug 2018, 2:03 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 3
You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. for example: System.out.print(str.contains ("love")); System.out.print (str.indexOf ("love") != -1); System.out.print (str.lastIndexOf ("love") != -1);
11th Aug 2018, 9:56 PM
Mohammed