Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search a string in another string? [duplicate]

Tags:

java

string

Possible Duplicate:
How to see if a substring exists inside another string in Java 1.4

How would I search for a string in another string?

This is an example of what I'm talking about:

String word = "cat"; String text = "The cat is on the table"; Boolean found;  found = findInString(word, text); //this method is what I want to know 

If the string "word" is in the string "text", the method "findInString(String, String)" returns true else it returns false.

like image 505
Meroelyth Avatar asked Feb 14 '12 11:02

Meroelyth


People also ask

How do you find duplicate characters in a string C++?

Algorithm. Define a string and take the string as input form the user. Two loops will be used to find the duplicate characters. Outer loop will be used to select a character and then initialize variable count by 1 its inside the outer loop so that the count is updated to 1 for every new character.

How do I find a specific string in a string?

Use the String. indexOf(String str) method. From the JavaDoc: Returns the index within this string of the first occurrence of the specified substring.


1 Answers

That is already in the String class:

String word = "cat"; String text = "The cat is on the table"; Boolean found;  found = text.contains(word); 
like image 199
Stephan Avatar answered Oct 10 '22 03:10

Stephan