Home » Java String startsWith() method

Java String startsWith() method

by Online Tutorials Library

Java String startsWith()

The Java String class startsWith() method checks if this string starts with the given prefix. It returns true if this string starts with the given prefix; else returns false.

Signature

The syntax or signature of startWith() method is given below.

Parameter

prefix : Sequence of character

offset: the index from where the matching of the string prefix starts.

Returns

true or false

Internal implementation of startsWith(String prefix, int toffset)

Internal Implementation of startsWith(String prefix,)

Java String startsWith() method example

The startsWith() method considers the case-sensitivity of characters. Consider the following example.

FileName: StartsWithExample.java

Output:

true  true  false  

Java String startsWith(String prefix, int offset) Method Example

It is an overloaded method of the startWith() method that is used to pass an extra argument (offset) to the function. The method works from the passed offset. Let’s see an example.

FileName: StartsWithExample2.java

Output:

true  false  true  

Java String startsWith() Method Example – 3

If we adding an empty string at the beginning of a string, then it has no impact at all on the string.

“” + “Tokyo Olympics” = “Tokyo Olympics”s

It means one can say that a string in Java always starts with the empty string. Let’s confirm the same with the help of Java code.

FileName: StartsWithExample3.java

Output:

The string starts with the empty string.  

You may also like