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.
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.
Use the String. indexOf(String str) method. From the JavaDoc: Returns the index within this string of the first occurrence of the specified substring.
That is already in the String class:
String word = "cat"; String text = "The cat is on the table"; Boolean found; found = text.contains(word);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With