The .startsWith() method returns true if a string starts with a given character sequence. Otherwise, false is returned.



The stringOfChcaracters can either be a string literal or a representation of a String value (variables, constants, etc). This method will only return case-sensitive matches (i.e. “Word” != “word”).


The following example demonstrates the .startsWith() method:

import java.io.*;
public class Main {
public static void main(String[] args) {
// With string literals
System.out.println("Hello World".startsWith("ello"));
// With string objects
String insertBadCoffeePun = "Java";
String noMoreJokes = "J";
System.out.println("We have had enough of bad coffee puns: " + insertBadCoffeePun.startsWith(noMoreJokes));

This will print the following output:

We have had enough of bad coffee puns: true


Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Java on Codecademy