BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String startsWith() Method with examples

Last Updated: September 16, 2022 by Chaitanya Singh | Filed Under: java

The startsWith() method of String class is used for checking prefix of a String. It returns a boolean value true or false based on whether the given string starts with the specified letter or word.

For example:

String str = "Hello";

//This will return true because string str starts with "He"
str.startsWith("He"); 

Java String startsWith() method Examples

There are two variations of starsWith() method.

boolean startsWith(String str): It returns true if the String str is a prefix of the String.

boolean startsWith(String str, index fromIndex): It returns true if the String begins with str, it starts looking from the specified index “fromIndex”.

For example lets say that the value of the String str is “Hi there” and we are calling starsWith() method like this – str.startsWith(“there”, 3) then this will return true because we have provided the fromIndex value as 3. The method will start looking from index 3 and it will find the string from where it starts searching.

Example 1: Checking prefix without fromIndex

This is a simple example where we have a string s and we are checking wether the string s starts with a particular word using startsWith() method.

public class JavaExample{  
  public static void main(String args[]){ 
    //given string
    String s = "This is just a sample string";  
		
    //checking whether the given string starts with "This"
    System.out.println(s.startsWith("This"));  
		
     //checking whether the given string starts with "Hi"
     System.out.println(s.startsWith("Hi"));  
   }
}

Output:
Java String startsWith method example

Example 2: Checking prefix starting from an index

Let’s take an example where we are using both the variations of startsWith() method.  As you can see that when we provided the fromIndex, the output changed. This is because it starts looking from the specified fromIndex.

This means:

  • If str.startsWith("brown") then it is checking the prefix of this string: quick brown fox jumps over the lazy dog
  • If str.StartsWith("brown", 6) then it is checking the prefix of this string: brown fox jumps over the lazy dog
public class StartsWithExample{
   public static void main(String args[]) {
       String str= new String("quick brown fox jumps over the lazy dog");
       System.out.println("String str starts with quick: "+str.startsWith("quick"));
       System.out.println("String str starts with brown: "+str.startsWith("brown"));
       System.out.println("substring of str(starting from 6th index) has brown prefix: "
+str.startsWith("brown", 6));
       System.out.println("substring of str(starting from 6th index) has quick prefix: "
+str.startsWith("quick", 6));

   }
}

Output:

String str starts with quick: true
String str starts with brown: false
substring of str(starting from 6th index) has brown prefix: true
substring of str(starting from 6th index) has quick prefix: false

Example 3: Whether String starts with an empty string

An empty string is often referred as “” (no space between double quotes). We are checking whether every string has empty string as a prefix. We can simply check this by calling startsWith() method like this: str.startsWith("").

public class JavaExample
{
  public static void main(String args[])
  {

    String str = "Welcome to BeginnersBook";

    // "" represents an empty string
    if(str.startsWith("")){
      System.out.println("String has empty string as prefix");
    }else{
      System.out.println("String doesn't have empty string as prefix");
    }
  }
}

Output:

String has empty string as prefix

Recommended Posts

  • Java String endsWith() method
  • Java String isEmpty() method
  • Java String join() method
  • Java String valueOf() method
❮ Java String

Top Related Articles:

  1. Java 8 StringJoiner with example
  2. Java String indexOf() Method
  3. Java String substring() Method with examples
  4. ArrayList clone() method in Java
  5. Java String equals() and equalsIgnoreCase() Methods example

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Srihitha says

    December 21, 2016 at 7:13 PM

    will startsWith method return true if the input is:
    search string-aab
    list-{bcf , nhgggg , aabde}
    As per my knowledge it should return true as aabde contains aab.
    Am i right?

    Reply
  2. Prabhu Kiran says

    March 14, 2018 at 5:23 PM

    Will it check starting word or starting letter…??

    Reply
    • Chaitanya Singh says

      February 3, 2019 at 10:54 AM

      It depends on what you are passing in the startsWith() method, for example if you are calling the method like this: str.startsWith(“A”) then it will check whether the string str starts with letter ‘A’ or not.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap