Java startsWith Method

The Java startsWith method is one of the Java String Methods, which is to check whether the string is starting with 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 in Java Programming language with example. The syntax of the string.startsWith in Java Programming language is as shown below.

Java startsWith method 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 is starting 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 method will accept the substring as the first parameter and Starting index position (Starting_index) as the second argument.

Java String.startsWith 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)
  • String_Object: Please specify the valid String Object.
  • Starting_Index: Please specify the starting index position. If we specify this value, then instead of looking from index position 0 java String.startsWith function will start looking from this position.

Java startsWith Example

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

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

// Java String.startsWith example
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

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

boolean a = str.startsWith("Tut");

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

boolean c = str1.startsWith("abc");

The below 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 startsWith function will return TRUE, otherwise False.

boolean d = str1.startsWith("abc", 15);

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

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 Example

In this Java program, We are going to ask the user to enter any word. Based on the user entered string value, the startsWith program will display the message.

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 String Library");
		}
		else {
			System.out.println("Goodbye to String Library");
		}
	}
}
Please Enter any word: 
java
Hey!! Welcome to String Library

Let us enter a different word

Please Enter any word: 
sql
Goodbye to String Library

Within this startsWith method example, the following statement will ask the user to enter any word. Then we are going to assign the user entered value to string variable str.

System.out.println("Please Enter any word: ");
String str = sc.nextLine();

Next, we used the Java If Else Statement to check where the user entered string is starting with “ja” or not.

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