where s is the substring.
The return value is boolean. It is either true or false. If the string contains the character sequence, true is returned. If it does not contain the character sequence, false is returned. One occurrence of the character sequence (substring) suffices to return true. This means it is the first occurrence that matters.
This is not a static method. So, a string object has to be instantiated before the method can be used.
The Java String class does not have any method with the name find() or search(). In other languages, the string class has the method find() or search() that serves the same purpose, which this Java contains() method serves.
In Java, the String class is in the Java.lang package. This package is automatically imported by the Java compiler. It does not have to be imported by the programmer with a statement.
This article shows how to answer the question: does a Java string contains a particular substring (character sequence)? – And if yes, how to delete or replace the substring. Searching in the String class in Java is case-sensitive.
Using the contains() Method
The following program illustrates how to use the contains() method:
The output is true. There is only one class in the program with the main() method. The main() method has all the code. The first statement in the main() method has the instantiated string with two of the same character sequence, “world”. The second statement uses the string contains() method. It checks if “world” is found in the string. If it is found, it returns true. If it is not found, it returns false. The third statement prints out the boolean value returned (in this case, true).
The following program outputs false because “earth” is not found in the string:
A character sequence, “world”, to look for, can be a variable. The following program illustrates this:
The output is true. The character sequence, “earth”, that resulted in false can also be a variable. The following program illustrates this:
The output is false.
Deleting SubString Found
After it has been found that a string contains a character sequence, the character sequence (substring) can be deleted. The Java String class does not have a delete or remove method for a substring found. However, there is a workaround: The Java String class has the replace() method. The replace() method does not need the contains() method. It searches for the character sequence and replaces it. What is found can be replaced with the empty string, and that is deleted. The full syntax to replace the first occurrence is:
where the target is the character sequence to look for, and replacement is the substitute. In this case, replacement is the empty string, “”. The method is not a static method. This means that the string literal must be of the instantiated String class. The method returns a second string with the substring deleted.
The following program illustrates how to delete all of the same substrings that are contained in a string:
The output is:
The continuous to become a global village. Our is a planet.
The original string remains unchanged. Notice that there are double spaces after “The” and after “Our” in the modified string. This means that either the space in front of “world” or after “world” also had to be deleted. The following program illustrates this:
The output is now satisfactory, as follows:
The continuous to become a global village. Our is a planet.
Replacing All Substrings Found
The same character sequences found can be replaced by the same substring. The approach is the same as the above, but instead of using an empty string, a string with text is used. The following program shows how to do this:
The output is:
The earth continuous to become a global village. Our earth is a planet.
The substring is searched, and the replacement does not have to be the same length.
Conclusion
In order to know if a string contains a character sequence, use the String method, str.contain(), where str is an instantiation of the String class. In order to search and replace all of the same substrings, use the str.replace() method.