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.
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.
/** * 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? falseSince 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.
/** * 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: trueSince 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.
/** * 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: trueThe 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())); } }
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: trueHere 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: trueHere 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