Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
About String startsWith()
2.1.
Parameters of String startsWith()
2.2.
Syntax of String startsWith()
2.2.1.
If offset is not given
2.2.2.
If offset is given
2.3.
Return value of String startsWith()
3.
Implementation of String startswith() without offset
4.
Implementation of String startswith() with offset
5.
Frequently Asked Questions
5.1.
How can we check if a string ends with a certain character?
5.2.
Where can we use string startsWith()? 
5.3.
What value is returned if the offset value is larger than the string size?
6.
Conclusion
Last Updated: Mar 27, 2024

String startsWith() in Java

Author Md Yawar
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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.

About String startsWith()

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 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()

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()

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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);
    }
}

 

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);
    }
}

 

Output:

true
false
true
false
true

Frequently Asked Questions

How can we check if a string ends with a certain character?

We can use String endsWith() to check whether a string starts with a certain character or not.

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 value is returned if the offset value is larger than the string size?

It returns false if the offset value is larger than the string size.

Conclusion

This blog talked about string startsWith() method and implemented the string substring() method in Java. 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine learning, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Live masterclass