⬅ Previous Topic
Java Stringconcat()
methodNext Topic ⮕
Java StringcontentEquals()
methodcontains()
method⬅ Previous Topic
Java Stringconcat()
methodNext Topic ⮕
Java StringcontentEquals()
methodThe 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.
public boolean contains(CharSequence s)
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. |
The contains()
method returns true
if the specified character sequence is found within this string; otherwise, it returns false
.
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.
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
.
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
.
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.
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.
⬅ Previous Topic
Java Stringconcat()
methodNext Topic ⮕
Java StringcontentEquals()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.