Pages

Thursday, January 6, 2022

Java String startsWith() Examples

1. Overview

In this article, You'll learn how to Test if this string starts with the specified prefix

Method startsWith() is case-sensitive.

String API is added with a method startsWith() that returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the equals(Object) method.

This method works exactly opposite to the String endsWith() method.

Java String startsWith() Examples


2. String startsWith() Syntax


Below is syntax from api.
public boolean startsWith​(String prefix)

public int indexOf​(String str, int fromIndex)
This method takes String as an argument. The current string will be checked with the prefix string that will be started or not.

If the prefix is null then it will not throw NullPointerException.

This method is overloaded that takes the index from which position startwith method should be applied.

3. String startsWith() Examples


package com.javaprogramto.strings;

public class StringStartsWithExample {

    public static void main(String[] args) {

        String s1 = "hello world";

        boolean isStringStartsWithHello = s1.startsWith("hello");

        System.out.println("String starts with hello or not : "+isStringStartsWithHello);
        System.out.println("Phone number starts with +91 "+"+91 12345678".startsWith("+678"));

        System.out.println("javaprogramto.com starts with java : "+"javaprogramto.com".startsWith("java"));
    }
}

Output:

String starts with hello or not : true
Phone number starts with +91 false
javaprogramto.com starts with java : true

4. String startsWith(String, fromIndex) Examples


package com.javaprogramto.strings;

public class StringStartsWithIndexExample {

    public static void main(String[] args) {

        String s1 = "hello world";

        boolean isStringStartsWithHello = s1.startsWith("hello", 5);

        System.out.println("String starts with hello or not : "+isStringStartsWithHello);
        System.out.println("Phone number starts with +91 "+"+91 12345678".startsWith("+678", 2));

        System.out.println("javaprogramto.com starts with java : "+"javaprogramto.com".startsWith("program",4));

        System.out.println("starts with index : "+"phone: +91 123456789".startsWith("+91", 7));
    }
}
Output:

String starts with hello or not : false
Phone number starts with +91 false
javaprogramto.com starts with java : true
starts with index : true 

5. String startsWith() Case Sensitive Example


There are some cases where you do not know whether the input string is upper case or lowercase and may not know the exact string.

In those cases, you need to be careful when passing the right prefix to the method.

package com.javaprogramto.strings;

public class StringStartsWithCaseSensitiveExample {

    public static void main(String[] args) {

        System.out.println("Case senstive with upper case values : "+"HELLO WORLD".startsWith("hello"));

        System.out.println("how to handle case sensitives: "+"HELLO WORLD".toLowerCase().startsWith("hello"));

    }
}

Output:

Case senstive with upper case values : false
how to handle case sensitives: true
If you are not sure about the prefix case then just convert the string to upper case or lower case and then call startsWith() method as shown in the above example.

6. Conclusion


In this article, you have seen how to work with startWith() in java strings along with examples.

All examples are over GitHub.

No comments:

Post a Comment

Please do not add any spam links in the comments section.