Java startsWith Method

The Java startsWith method is one of the String Methods, which is to check whether the string is starting with a user-specified substring or not. Based on the result, the startsWith method will return Boolean True or False.

In this article, we will show how to write the startsWith with an example. The syntax of the string startsWith in Java Programming language, as shown below.

startsWith Syntax

The Java Programming Language provides two different startsWith methods. The following method will accept the substring as the argument and check whether the string starts with this substring or not.

public boolean startsWIth(String Prefix); // It will return boolean True or False 

//In order to use in program
String_Object.startsWIth(String Prefix)

The following Java startsWith method will accept the substring as the first parameter and Starting index position (Starting_index) as the second argument.

This method will start looking from the Starting_index and check whether the string at Starting_index is starting with this substring or not.

public boolean startsWIth(String Prefix, int Starting_Index); // It will return boolean True or False 

//In order to use in program
String_Object.startsWIth(String Prefix, int Starting_Index)
  • Starting_Index: Please specify the starting index position. If we specify this value, then instead of looking from index position 0, the String.startsWith function will start looking from this position.

Java startsWith Example

In this program, We are going to use the startsWith string method to check whether the string is starting with a user-specified substring or not.

If you specify the Starting_Index as a negative value or greater than string length, the function will return boolean False as output.

For boolean a, it will call the public boolean startsWith (String Prefix) method to check whether the str is starting with Tut or not. If it is TRUE, then it will return TRUE else, False.

The following Java startsWith statement will check whether the str1 is starting with abc or not. If it is TRUE, it will return TRUE; otherwise False.

The below d String Method statement will call the public boolean startsWith (String Prefix, int Starting_Index) method to check whether the index position 15 in str1 is starting with abc or not. If it is TRUE, the function will return TRUE, otherwise False.

The following Java System.out.println statements will print the output.

package StringFunctions;

public class StartsWithMethod {
	public static void main(String[] args) {
		String str = "Tutorials On Java Programming";
		String str1 = "abc working at abc company";
		
		boolean a = str.startsWith("Tut");
		boolean b = str.startsWith("tut");
		boolean c = str1.startsWith("abc");
		boolean d = str1.startsWith("abc", 15);

		System.out.println("Does the String str Starts with Tut? = " + a);
		System.out.println("Does the String str Starts with tut? = " + b);
		System.out.println("Does the String str1 Starts with abc? = " + c);
		System.out.println("Starting from index 15 - Does the String str1 Starts with abc? = " + d);
	}
}
Java startsWith Method 1

Java startsWith Example

In this Java program, We are going to ask the user to enter any word. This program will display the message based on the user’s entered string value.

Within this method example, the first two statements will ask the user to enter any word. Then we are going to assign the user entered value to an str variable.

Next, we used the If Else Statement to check whether the user entered one starting with “ja” or not.

  • If it True System.out.println(“Hey!! Welcome to Library”); statement will be printed.
  • Else System.out.println(“Goodbye to Library”); statement will be printed.
package StringFunctions;

import java.util.Scanner;

public class StartsWithMethod2 {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.println("Please Enter any word: ");
		String str = sc.nextLine();
		
		if (str.startsWith("ja")) {
			System.out.println("Hey!! Welcome to Library");
		}
		else {
			System.out.println("Goodbye to Library");
		}
	}
}
Please Enter any word: 
java
Hey!! Welcome to Library

Let us enter a different word.

Please Enter any word: 
sql
Goodbye to Library