StringUtils startsWith() example in Java

In this post, we will see org.apache.commons.lang3.StringUtils startsWith() example in Java. The startsWith() method is used to check whether a given character sequence starts with another character sequence or not. It returns true if the given character starts with another character else returns false.

We need to add the below dependency in maven to use org.apache.commons.lang3.StringUtils startsWith () method. We can download apache-commons maven dependency from here.

pom.xml changes

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>

Note – The StringUtils startsWith() method internally uses CharSequenceUtils.regionMatches() method.

StringUtils startsWith() example in Java

package com.javatute;

import org.apache.commons.lang3.StringUtils;

public class StringUtilsIsStartsWith {
    public static void main(String[] args) {
        System.out.println(StringUtils.startsWith("javaProgram", "ja"));
        System.out.println(StringUtils.startsWith("hi", "@"));
        System.out.println(StringUtils.startsWith("star", "st"));
        System.out.println(StringUtils.startsWith("jbl", "book"));
        System.out.println(StringUtils.startsWith("5241", "524"));
        System.out.println(StringUtils.startsWith("java", "javai21"));

    }
}

Output is

true
false
true
false
true
false

That’s all about StringUtils startsWith() example in Java.

Summary – We have seen that startsWith method is used to check given char sequence starts with the given prefix or not.

Few questions related to startsWith() method.

How to check if a String is beginning with some character Java?

The StringUtils startsWith() method is used to check whether a string starts with the given character. We can also use the String startsWith() method to check the weather a string starts with a character or not.

Is startsWith () case sensitive?

Yes, StringUtils startsWith() method is case sensitive. For example below program will return false

import org.apache.commons.lang3.StringUtils;

public class SpringMain {
    public static void main(String[] args) {
        System.out.println(StringUtils.startsWith("CAT program", "cat"));
    }
}

Output is – false

See docs.

Related post.