The Java startsWith method is one of the Java String Method which is used to check whether the string is starting with user specified substring or not and based on the result it will return Boolean True or False.
In this article we will show you, How to write startsWith in Java Programming language with example. Before we get into the example, let us see the basic 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. 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)
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 you specify this value then, instead of looking from index position 0 java String.startsWith function will start looking from this position.
NOTE: If you specify the Starting_Index as a negative value or greater than string length, Java String.startsWith function will return boolean False as output.
Java startsWith Example
The Java string startsWith method is used to check whether the string is starting with user specified substring or not. In this Java program, We are going to find the same.
JAVA CODE
// 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); } }
OUTPUT
ANALYSIS
In this Java startsWith example, First we declared two String variables Str, Str1 and assigned corresponding values using following statement
String str = "Tutorials On Java Programming"; String str1 = "abc working at abc company";
Following statement 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");
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");
Below 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, function will return TRUE, otherwise False
boolean d = str1.startsWith("abc", 15);
Following 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 and based the user entered string value we will display the message.
JAVA CODE
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"); } } }
OUTPUT
Let us enter different word
ANALYSIS
Within this Java startsWith method example, the following statement will ask the user to enter any word, and 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
Thank You for Visiting Our Blog