Java String startsWith() Method with Examples Last Updated : 22 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, the startsWith() method of the String class is used to check if a string starts with the given prefix. The startsWith() method is present in the java.lang package. In this article, we will learn how to use the startsWith() method in Java.Example:In the below example, we will use the startsWith() method to check if the string starts with a given prefix. Java // Java program to demonstrate startsWith() method public class StringStartsWith { public static void main(String[] args) { // Declare and initialize a String String s = "Java Programming"; // Check if the string starts with "Java" boolean r = s.startsWith("Java"); System.out.println("" + r); } } Outputtrue Table of ContentSyntax of startsWith() methodOther Examples of startsWith() MethodCase Sensitivity ExampleChecking with a Different PrefixUsing startsWith() with IndexSyntax of startsWith() methodboolean startsWith(String prefix)Parameter: prefix: The prefix to be checked.Return Types: It returns true, if the string starts with the specified prefix, otherwise false.Other Examples of startsWith() Method1. Check with Case SensitivityThe startsWith() method is case-sensitive. This means it will return false if the case of the characters does not match.Example: Java // Java program to demonstrate case sensitivity of startsWith() public class StringStartsWith { public static void main(String[] args) { // Declare and initialize a String String s = "Java Programming"; // Check if the string starts with "java" (lowercase) boolean r = s.startsWith("java"); System.out.println("" + r); } } Outputfalse 2. Check with a Different PrefixWe can use startsWith() method to check if the string starts with any other prefix.Example: Java // Java program to demonstrate startsWith() with a different prefix public class StringStartsWith { public static void main(String[] args) { // Declare and initialize a String String s = "Java Programming"; // Check if the string starts with "Java" boolean r = s.startsWith("Java"); System.out.println("" + r); } } Outputtrue 3. Using startsWith() with IndexWe can also specify an index in the startsWith() method. It checks whether the substring starting from that index begins with the specified prefix.Example: Java // Java program to demonstrate startsWith() with index public class StringStartsWith { public static void main(String[] args) { // Declare and initialize a String String s = "Java Programming"; // Check if the string starting from index 5 starts with "Pro" boolean r = s.startsWith("Pro", 5); System.out.println("" + r); } } Outputtrue Comment More infoAdvertise with us Next Article StringBuffer substring() method in Java with Examples A Astha Tyagi Improve Article Tags : Java Java-Strings Java-lang package Java-Functions Practice Tags : JavaJava-Strings Similar Reads Path startsWith() method in Java with Examples startsWith() method of java.nio.file.Path used to check if this path object starts with the given path or string which we passed as parameter. There are two types of startsWith() methods. startsWith(String other) method of java.nio.file.Path used to check if this path starts with a Path, constructed 3 min read Matcher start(String) method in Java with Examples The start(String string) method of Matcher Class is used to get the start index of the match result already done, from the specified string. Syntax: public int start(String string) Parameters: This method takes a parameter string which is the String from which the start index of the matched pattern 2 min read StringBuffer substring() method in Java with Examples In StringBuffer class, there are two types of substring method depending upon the parameters passed to it. substring(int start) The substring(int start) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to end of this sequence. The strin 4 min read Pattern quote(String) method in Java with Examples quote(String) method of a Pattern class used to returns a literal pattern String for the specified String passed as parameter to method.This method produces a String equivalent to s that can be used to create a Pattern. Metacharacters or escape sequences in the input sequence will be given no specia 3 min read CompoundName startsWith() method in Java with Examples The startsWith() method of a javax.naming.CompoundName class is used to check whether compound name which is passed as a parameter is a prefix of this compound name or not. A compound name 'X' is a prefix of this compound name if this compound name object ends with 'X'. If X is null or not a compoun 2 min read Java String subSequence() Method with Examples In Java, the String class provides the subSequence() method. This subSequence() method extracts a portion of a string as a CharSequence. This method is useful for retrieving specific character ranges from a string which enhances string manipulation. In this article, we will learn how to use the subS 3 min read Like