Home  >  Article  >  Java  >  How to check if a string starts with a specific substring in Java?

How to check if a string starts with a specific substring in Java?

PHPz
PHPzforward
2023-09-09 19:13:041106browse

How to check if a string starts with a specific substring in Java?

String class can be used to represent strings. All string literals in Java programs are implemented as an instance A string class. Strings are constants, and their values ​​cannot be changed once created (immutable).

We can use the startsWith(String class) method to check if a string starts with a specific string, it returns a boolean value, true or false .

Syntax
public boolean startsWith(String prefix)

Example

public class StringStartsWithSubStringTest {
   public static void main(String[] args) {
      String str = "WelcomeToTutorialsPoint";
      if(str.startsWith("<strong>Welcome</strong>")) {
         System.out.println("Begins with the specified word!");
      }
      else {
         System.out.println("Does not begin with the specified word!");
      }
   }
}

Output

Begins with the specified word!

The above is the detailed content of How to check if a string starts with a specific substring in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete