Home >>Java String Methods >Java String startsWith() Method

Java String startsWith() Method

Java String startsWith() Method

Java startsWith() method in Java String is used to checks whether a string starts with the specified character.

Syntax

public boolean startsWith(String chars)

Parameters

char - This parameter is used to represents A String of the character to check for

Technical Details

Returns - It is used to returns a boolean value:

  • true - It returns true, if the string starts with the specified character
  • false - It returns false, if the string does not start with the specified character

Java String startsWith() Method Example 1


public class StartsWithExample1 
{
  public static void main(String[] args) 
  {
    String Name = "Phptpoint";
    System.out.println(Name.startsWith("Php"));
    System.out.println(Name.startsWith("ptpo"));
    System.out.println(Name.startsWith("int"));
  }
}


Output:
true
false
false

Java String startsWith() Method Example 2


public class StartsWithExample1 
{  
    public static void main(String[] args) 
	{  
        String n1 = "Phptpoint";  
        System.out.println(n1.startsWith("P"));   
        System.out.println(n1.startsWith("t"));   
        System.out.println(n1.startsWith("i",1));  
    }  
}

Output:
true
false
false

No Sidebar ads