Java - String startsWith() Method
The Java string startsWith() method is used to check whether the substring of this string beginning at the specified index starts with the specified prefix. It returns true when the substring starts with specified prefix, else returns false.
Syntax
public boolean startsWith(String prefix, int toffset)
Parameters
prefix |
Specify the prefix. |
toffset |
Specify the offset position where to begin looking in this string. |
Return Value
Returns true if the substring starts with specified prefix, else returns false.
Exception
NA.
Example:
In the example below, startsWith() method is used to check whether the substring of 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!."; //checking whether the substring starts with "World" System.out.println(MyString.startsWith("Hello", 6)); System.out.println(MyString.startsWith("World", 6)); } }
The output of the above code will be:
false true
❮ Java.lang - String