Java contains() Method Usage Guide
The “contains()” method in Java can be used to check if a string contains a specific character sequence. Here is how it is used:
- The general format for using the contains() method is as follows:
- boolean result = string.contains(sequence);
- The string is the string to be checked, and the character sequence to be checked is the character sequence to be searched. The contains() method will return a boolean value: true if the string contains the character sequence to be checked, and false otherwise.
- Example:
– Give me an example.
- String str = “Hello World”;
boolean contains = str.contains(“Hello”);
System.out.println(contains); // The output is trueboolean contains2 = str.contains(“Java”);
System.out.println(contains2); // The output is false
When using the contains() method, you can choose to use either variables or directly use string constants as the character sequence to be checked.