Languages
[Edit]
EN

Java - string startsWith() method example

0 points
Created by:
Dirask Community
6300

In this article, we would like to show you startsWith() method example in Java.

Quick solution:

String string = "Dirask is awesome.";

System.out.println(string.startsWith("Dirask "));  // true
System.out.println(string.startsWith("is"));       // false

 

1. Documentation

Syntaxpublic boolean startsWith(String prefix)
Parametersprefix - the string representation of the character to be checked
Resulttrue - if the string starts with the specified character or word,
false - if the string doesn't start with the specified character or word.
Description

Tests if the string starts with the specified prefix.

2. Practical example

In this example, we check if our string starts with a specified character or string.

public class Example {
    public static void main(String[] args) {
        String string = "Dirask is awesome.";

        System.out.println(string.startsWith("D"));         // true
        System.out.println(string.startsWith("Dirask"));    // true

        System.out.println(string.startsWith("X"));         // false
        System.out.println(string.startsWith("is"));        // false
    }
}

Output:

true
true
false
false

Java - String documentation

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join