Home > Java > javaTutorial > 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
Release: 2023-09-09 19:13:04
forward
1153 people have browsed it

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)
Copy after login

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!");
      }
   }
}
Copy after login

Output

Begins with the specified word!
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template