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:

  1. The general format for using the contains() method is as follows:
  2. boolean result = string.contains(sequence);
  3. 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.
  4. Example:

    – Give me an example.

  5. String str = “Hello World”;
    boolean contains = str.contains(“Hello”);
    System.out.println(contains); // The output is true

    boolean 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.

bannerAds