is a common scenario in programming when we wish to check if one String contains a specific substring. For example, we wish to test if the String “The quick brown fox” contains the substring “brown”. The Java String class has a method contains() that can help with this case. Below are examples on how to use Java String’s contains method.
Syntax

The Java String class has the method contains() to check if a String contains a specified character sequence. The syntax is shown below:

public boolean contains(CharSequence s);

The method will check if the given parameter is a substring on the String instance and returns a boolean value. If the character sequence is found, it will return true, otherwise false is returned.
Implementation

Peeking at the implementation of this method as of Java 1.5, below is the details:

public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}

As shown, it converts the CharSequence to a String and then calls the indexOf method. The method indexOf returns 0 or higher number if it finds the String, otherwise -1 is returned.
Simple Example

Below is a simple example on how to use the contains method of the Java String class:

/**
* A simple example that shows if one String contains another substring.
*/
public class Test {
public static void main(String[] args) {
String phrase = "The quick brown fox";
String colorBrown = "brown";
String colorRed = "red";
System.out.println("The phrase is: " + phrase);
System.out.println("The phrase contains the color brown? " + phrase.contains(colorBrown));
System.out.println("The phrase contains the color red? " + phrase.contains(colorRed));
}
}

When we run this program, the output is shown below:

The phrase is: The quick brown fox
The phrase contains the color brown? true
The phrase contains the color red? false

Since the String “brown” is in the phrase, the output of the frist test is true. And because “red” is not in the phrase, the output of the second test is false.
Case Sensitivity

One pitfall that we should consider is that the contains method is case sensitive. Which means the String to be searched must match including the case, for each letter! Here is a simple example that demonstrates this:

/**
* A simple example that shows String contains method is case sensitive.
*/
public class Test {
public static void main(String[] args) {
String phrase = "The quick brown fox";
System.out.println("First test: " + phrase.contains("Brown"));
System.out.println("Second test: " + phrase.contains("brown"));
}
}

The output is shown below:

First test: false
Second test: true

Since the phrase has the color brown in all lower case, the first test will fail because it has a capital letter. While the second test will pass because it matches letter by letter and case by case.
Case Insensitive

If we wish to search if a String contains a character sequence, but in case insensitive way, we may adjust our code for this. We may convert both Strings to uppercase or to lowercase before doing the test. Here is an example:

/**
* A simple example that demonstrate case insensitive matching using contains.
*/
public class Test {
public static void main(String[] args) {
String phrase = "The quick brown fox";
String color = "BrOwn";
System.out.println("First test: " + phrase.contains(color));
System.out.println("Second test: " + phrase.toLowerCase().contains(color.toLowerCase()));
}
}

The output will be:

First test: false
Second test: true

The solution is used in the second test. Since the color to be searched is in mixed case, when we convert both to lower case before comparing, it becomes case insensitive. We will get similar results if we use upper case. Example:

/**
* A simple example that demonstrate case insensitive matching using contains.
*/
public class Test {
public static void main(String[] args) {
String phrase = "The quick brown fox";
String color = "BrOwn";
System.out.println("First test: " + phrase.contains(color));
System.out.println("Second test: " + phrase.toUpperCase().contains(color.toUpperCase()));
}
}

CharSequence

Since contains can accept any implementation of CharSequence, we are not restricted on passing only String parameter. Below are some examples on how to use with different implementation of CharSequence.

Here is a simple example of using Java String’s contain method against a CharBuffer parameter:

import java.nio.CharBuffer;
/**
* A simple example that shows how to use contains with CharBuffer.
*/
public class Test {
public static void main(String[] args) {
String phrase = "The quick brown fox";
CharBuffer cb = CharBuffer.allocate(5);
cb.append('b');
cb.append('r');
cb.append('o');
cb.append('w');
cb.append('n');
cb.clear();
System.out.println("Char Buffer contents: " + cb.toString());
System.out.println("Contains result: " + phrase.contains(cb));
}
}

The output will be:

Char Buffer contents: brown
Contains result: true

Here is a simple example of using Java String’s contain method against a StringBuilder parameter:

/**
* A simple example that shows how to use contains with StringBuilder.
*/
public class Test {
public static void main(String[] args) {
String phrase = "The quick brown fox";
StringBuilder sb = new StringBuilder();
sb.append("b");
sb.append("r");
sb.append("o");
sb.append("w");
sb.append("n");
System.out.println("StringBuilder contents: " + sb.toString());
System.out.println("Contains result: " + phrase.contains(sb));
}
}

The code will display:

StringBuilder contents: brown
Contains result: true

Here is a simple example of using Java String’s contain method against a StringBuffer parameter:

/**
* A simple example that shows how to use contains with StringBuffer.
*/
public class Test {
public static void main(String[] args) {
String phrase = "The quick brown fox";
StringBuffer sb = new StringBuffer();
sb.append("b");
sb.append("r");
sb.append("o");
sb.append("w");
sb.append("n");
System.out.println("StringBuffer contents: " + sb.toString());
System.out.println("Contains result: " + phrase.contains(sb));
}
}

The result will be:

StringBuffer contents: brown
Contains result: true