Introduction
The contains()
method in Java is a handy tool for checking if one string exists within another. Think of it like searching for a specific word in a book – the contains()
method tells you whether that word is present or not.
Syntax
public boolean contains(CharSequence s)
Parameters
Parameter | Description |
---|---|
s |
The sequence of characters to search for within this string. It can be a String, Character Array or any other CharSequence implementation. |
Return Value
The contains()
method returns true
if the specified character sequence is found within this string; otherwise, it returns false
.
Examples
Example 1: Basic String Search
This example demonstrates how to use contains()
to check if a simple string exists within another string. We'll look for the word 'Java'.
public class Main {
public static void main(String[] args) {
String text = "This is a Java tutorial.";
boolean containsJava = text.contains("Java");
System.out.println(containsJava);
}
}
true
In this example, the text
variable holds a string. The contains()
method checks if 'Java' is present within text
. Because it is, the method returns true
and that value gets printed to the console.
Example 2: Case Sensitivity
The contains()
method is case-sensitive. This example shows what happens when we search for a string with different capitalization.
public class Main {
public static void main(String[] args) {
String text = "This is a Java tutorial.";
boolean containsJavaLower = text.contains("java");
System.out.println(containsJavaLower);
}
}
false
Here, we're searching for 'java' (lowercase). Because the original string contains 'Java' (uppercase), the method returns false
.
Example 3: Using a Character Array
The contains()
method can also accept a character array as input. This example demonstrates that use case.
public class Main {
public static void main(String[] args) {
String text = "Hello World";
char[] chars = {'H', 'e'}; //Character array to search for.
boolean containsChars = text.contains(chars);
System.out.println(containsChars);
}
}
true
We're searching the string text
, for a character array that represents 'He'. As it is present in the text, the method returns true
.
Example 4: Checking for Empty String
This example explores what happens when we attempt to search for an empty string within another string. An empty string is represented by "".
public class Main {
public static void main(String[] args) {
String text = "Hello World";
boolean containsEmpty = text.contains("");
System.out.println(containsEmpty);
}
}
true
The method always returns true
when searching for an empty string because every string contains a zero-length sequence at the beginning and after the last character.
Example 5: Using StringBuilder
This example demonstrates how to use contains()
with a StringBuilder object.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Java is awesome");
boolean containsAwesome = sb.contains("awesome");
System.out.println(containsAwesome);
}
}
true
The contains()
method can be used with a StringBuilder object like any other CharSequence.
Comments
Loading comments...