All Posts

Java startsWith() method

Java startsWith() method

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

Method signature

public boolean startsWith(String prefix)

prefix: The sequence of characters to check at the beginning of the string.
The prefix parameter is case-sensitive. If the prefix is an empty string (“”), the method will always return true because every string starts with an empty string.

Example

Let’s understand the startsWith() method using the help of an example program.

package com.testingdocs.stringmethods;

public class ExampleProgram {

	public static void main(String[] args) {
		String str = "Software Testing website TestingDocs.com";
		
        // startsWith() method demo
		System.out.println(str.startsWith("Soft"));
		System.out.println(str.startsWith("Testing"));
	}
}

 

Program Output

true

false

 

Java startswith method

Java Tutorials

Java Tutorial on this website: