Table of contents
1.
Introduction
2.
What is String startsWith() in Java?
3.
Parameters of Java String startsWith()
4.
Syntax of String startsWith() in Java
4.1.
If offset is not given
4.2.
If offset is given
5.
Return value of String startsWith() in Java
6.
Implementation of String startswith() without offset
7.
Implementation of String startswith() with offset
8.
Frequently Asked Questions
8.1.
Where can we use string startsWith()? 
8.2.
What does startsWith () do?
8.3.
Is startsWith case sensitive in Java?
8.4.
What is the difference between charAt and startsWith?
9.
Conclusion
Last Updated: Oct 7, 2024
Medium

String startsWith() in Java

Author Md Yawar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is one of the most widely used computer programming languages in the world. It is an object-oriented programming language with objects and classes. Java string class has many functions that can perform various operations on strings. String startswith() is a function that is used to check whether a string starts with a particular set of characters or not. Let us talk about string startsWith() in detail.

String startsWith() in Java

What is String startsWith() in Java?

The String startsWith() method is used to check whether a string starts with the given prefix. It returns a boolean value true if the string starts with the provided prefix else it returns false. It has two parameters -  prefix and offset.

Source

Parameters of Java String startsWith()

Prefix: Prefix is the sequence of characters that is to be matched with the given string to check whether it starts from it.

Offset: It gives us the starting position from where the matching of the string prefix start. It is optional and if not given, the matching will start from the first character.

Syntax of String startsWith() in Java

The syntax of String startsWith is:

If offset is not given

Public boolean startsWith(string prefix)

If offset is given

Public boolean startsWith(string prefix, int offset)

Return value of String startsWith() in Java

A boolean value that returns

true: if the string begins with the given prefix.

false: if the string does not begin with the given prefix.

Implementation of String startswith() without offset

import java.util.*;

class codingninjas {

    public static void main(String[] args)
    {
    
    //initialising the string
        String str = new String(
            "welcome to coding ninjas");
            
    //giving the prefix 
        boolean ans1 = str.startsWith("welcome");
        boolean ans2 = str.startsWith("coding");
        boolean ans3 = str.startsWith("we");
        boolean ans4 = str.startsWith("welcome to coding ninjas");
 
    // Printing the boolean value
        System.out.println(ans1);
        System.out.println(ans2);
        System.out.println(ans3);
        System.out.println(ans4);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

true
false
true
true

Implementation of String startswith() with offset

import java.util.*;
class codingninjas {

    public static void main(String[] args)
    {
    
    //initialising the string
        String str = new String(
            "welcome to coding ninjas");
            
    //giving the prefix 
        boolean ans1 = str.startsWith("ninjas",18);
        boolean ans2 = str.startsWith("ninjas",10);
        boolean ans3 = str.startsWith("coding ninjas", 11);
        boolean ans4 = str.startsWith("we", 1);
        boolean ans5 = str.startsWith("welcome to coding ninjas", 0);
 
    // Printing the boolean value
        System.out.println(ans1);
        System.out.println(ans2);
        System.out.println(ans3);
        System.out.println(ans4);
        System.out.println(ans5);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

true
false
true
false
true

Frequently Asked Questions

Where can we use string startsWith()? 

String startsWith() can be used to filter prefixes. For example, we can use String startsWith() to filter names according to a particular letter.

What does startsWith () do?

The startsWith() method in Java checks if a string begins with a specified prefix and returns true if it does, otherwise false.

Is startsWith case sensitive in Java?

Yes, startsWith() is case-sensitive in Java, meaning it distinguishes between uppercase and lowercase letters when matching the prefix.

What is the difference between charAt and startsWith?

charAt() retrieves a specific character at a given index in a string, while startsWith() checks if a string starts with a particular prefix. charAt() returns a character, and startsWith() returns a boolean.

Conclusion

The String startsWith() in Java is a simple and effective way to check if a string begins with a specified prefix. It returns a boolean, offering quick validation and pattern matching. Its flexibility with optional position arguments and ease of use make it a valuable tool for string manipulation, improving code readability and efficiency.

Refer to our Guided Path on Code360 to upskill yourself! If you want to test your competency in coding, you may check out the mock test series and participate in the contests.

Live masterclass