Java

Java String contains Method

When the programmer wants to know if a substring exists in a string, there comes a time. In Java, a substring is referred to as a character sequence. So, in Java, there are occasions when the programmer would want to know if a character sequence exists in a string. The Java contains() method is used for this. The full syntax is:

public boolean contains(CharSequence s)

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:

    public class TheClass {
        public static void main(String[] args) {
            String str = "The world continuous to become a global village. Our world is a planet.";
boolean bl = str.contains("world");
System.out.println(bl);
        }
    }

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:

    public class TheClass {
        public static void main(String[] args) {
            String str = "The world continuous to become a global village. Our world is a planet.";
boolean bl = str.contains("earth");
System.out.println(bl);
        }
    }

A character sequence, “world”, to look for, can be a variable. The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            String str = "The world continuous to become a global village. Our world is a planet.";
            String var = "world";
boolean bl = str.contains(var);
System.out.println(bl);
        }
    }

The output is true. The character sequence, “earth”, that resulted in false can also be a variable. The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            String str = "The world continuous to become a global village. Our world is a planet.";
            String var = "earth";
boolean bl = str.contains(var);
System.out.println(bl);
        }
    }

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:

public String replace(CharSequence target, CharSequence replacement)

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:

    public class TheClass {
        public static void main(String[] args) {
            String strOld = "The world continuous to become a global village. Our world is a planet.";
            String strNew = strOld.replace("world", "");
System.out.println(strOld);
System.out.println(strNew);
        }
    }

The output is:

The world continuous to become a global village. Our world is a planet.

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:

    public class TheClass {
        public static void main(String[] args) {
            String strOld = "The world continuous to become a global village. Our world is a planet.";
            String strNew = strOld.replace(" world", "");
System.out.println(strOld);
System.out.println(strNew);
        }
    }

The output is now satisfactory, as follows:

The world continuous to become a global village. Our world is a planet.

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:

    public class TheClass {
        public static void main(String[] args) {
            String strOld = "The world continuous to become a global village. Our world is a planet.";
            String strNew = strOld.replace("world", "earth");
System.out.println(strOld);
System.out.println(strNew);
        }
    }

The output is:

The world continuous to become a global village. Our world is a planet.

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.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.