English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The String contains() method checks whether the specified string (character sequence) exists in the string.
The syntax of the String contains() method is:
string.contains(CharSequence str)
The contains() method takes a single parameter.
str (charSequence)-Character sequence
Note: charSequence is a character sequence, for example: String, CharBuffer, StringBuffer, etc.
If the string contains the specified string str, thenReturns true
If the string does not contain the specified string str, thenReturns false
class Main { public static void main(String[] args) { String str1 = "Learn Java"; Boolean result; //Check str1Whether it contains "Java" result = str1.contains("Java"); System.out.println(result); // true //Check str1Whether it contains "Python" result = str1.contains("Python"); System.out.println(result); // false //Check str1Whether it contains "" result = str1.contains( System.out.println(result); // true } }
Here, string.contains(
class Main { public static void main(String[] args) { String str1 = "Learn Java"; String str2 = "Java"; String str3 = "java"; Boolean result; // returns true because "Learn Java" includes "Java" if (str1.contains(str2)) { System.out.println(str1 + " Does Include " + str2); } else { System.out.println(str1 + " Does Not Include " + str2); } // contains() is case-sensitive // returns false because "Learn Java" does not include "java" if (str1.contains(str3)) { System.out.println(str1 + " Does Include " + str3); } else { System.out.println(str1 + " Does Not Include " + str3); } } }
Output Result
Learn Java Includes Java Learn Java Does Not Include