string startswith() in java

In java, the string is the most common data type that is used in every program. The string startswith in java or string startswith() is used to check the particular string is starts from the given substring/string. The return type of string startswith() is boolean, It means the method returns true if the current string starts with suffix otherwise it returns false.
There are two variants of startswith() method:
1. String startsWith(String prefix) 2. String startsWith(String prefix, int startPosition)

string startswith in java

1. String startsWith(String prefix)

The return type of startsWith(String prefix) is boolean. It returns only true or false value. If this method takes only one parameter, the specified string beginning from the 0th index(First character).

stringName.startsWith(anotherStringName)

stringName: The name of string which will be checked whether it starts with the given String or not. anotherStringName:  It is a string that will be provided by the user to check whether the current string starts with the same string or not.

class ExampleOfStartsWith
{  
   public static void main(String args[])
   {    
	String string1 = "JAVA WINGS";  
	// It will returns true		
	System.out.println("Is Strings start with JAVA = "+ string1.startsWith("JAVA")); 
        // It will returns false
	System.out.println("Is Strings start with WINGS  = "+ string1.startsWith("WINGS")); 
        // It will returns true	
	System.out.println("Is Strings start with JA = "+ string1.startsWith("JA")); 
    }
} 

Output: Is Strings start with JAVA = true
Is Strings start with WINGS = false
Is Strings start with JA = true

2. String startsWith(String prefix, int startPosition)

The return type of startsWith(String prefix, int startPosition) is boolean. It returns only true or false value. It takes two parameters, One parameter is a string(prefix) and another is startPostion. It checks the current string starts with the given string(prefix) beginning a specified startPosition.
Let’s discuss the string startswith in java

stringName.startsWith(anotherStringName, 2)

stringName: The name of string which will be checked whether it starts with the given String or not
anotherStringName: It is a string that will be provided by a user to check whether the current string starts with the same string or not.

class ExampleOfStartsWith
{  
   public static void main(String args[])
   {    
     String string1 = "JAVA WINGS";  
     // It will returns false because JAVA start from index 0
     System.out.println("Is Strings start with JAVA = "+ string1.startsWith("JAVA", 2));   
     // It will returns true because WINGS start from index 5
     System.out.println("Is Strings start with WINGS  = "+ string1.startsWith("WINGS", 5));
     // It will returns true because JA start from index 0
     System.out.println("Is Strings start with JA = "+ string1.startsWith("JA", 0));	
  }
}  

Output: Is Strings start with JAVA = false
Is Strings start with WINGS = true
Is Strings start with JA = true

Leave a Comment