Java.lang Package Classes

Java - String startsWith() Method



The Java string startsWith() method is used to check whether the string starts with specified prefix or not. It returns true when the string starts with specified prefix, else returns false.

Syntax

public boolean startsWith(String prefix)

Parameters

prefix Specify the prefix.

Return Value

Returns true if the string starts with specified prefix, else returns false.

Exception

NA.

Example:

In the example below, startsWith() method is used to check whether the string called MyString starts with specified prefix or not.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    String MyString = "Hello World!";

    System.out.println(MyString.startsWith("Hello")); 
    System.out.println(MyString.startsWith("world")); 
  }
}

The output of the above code will be:

true
false

❮ Java.lang - String