Java String startsWith() Method with Examples Last Updated : 22 Nov, 2024 Comments Improve Suggest changes 4 Likes 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 Create Quiz Comment A Astha Tyagi 4 Improve A Astha Tyagi 4 Improve Article Tags : Java Java-Strings Java-lang package Java-Functions Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like