Advertisement



< Prev
Next >



startsWith() method




As the name of the method goes, startsWith() method of String class determines if the value in a String object starts with a specified String or not.




Signature of startsWith() method


public boolean startsWith(String st)






startsWith() example


In the upcoming code, we have a String object initialized to a value Hello World and we are goint to check whether this String object starts with a specific String value or not.

// startsWith() method of String.


class StringStartsWith
{
public static void main(String[] ar)
{
String str1= new String("Hello World");
System.out.println("String is "  + str1);

boolean boo1 = str1.startsWith("He");
System.out.println("Does "+ str1 + " starts with He ? "+ boo1);  

boolean boo2 = str1.startsWith("Hello");
System.out.println("Does "+ str1 + " starts with Hello ? "+ boo2);

boolean boo3 = str1.startsWith("World");
System.out.println("Does "+ str1 + " starts with World ? "+ boo3);  
}
}


Output is :



String is Hello World
Does Hello World starts with He ? true
Does Hello World starts with Hello ? true
Does Hello World starts with World ? false


Program Analysis







Method startsWith() is case-sensitive.


Method startsWith() checks for comparison between String value passed to it and the invoked String object, conforming to the case sensitivity of each letter.

// startsWith() method of String.


class StringStartsWith
{
public static void main(String[] ar)
{
String str1= new String("World Peace");
System.out.println("String is "  + str1);

boolean boo1 = str1.startsWith("World");
System.out.println("Does "+ str1 + " starts with He ? "+ boo1);  

boolean boo2 = str1.startsWith("world");
System.out.println("Does "+ str1 + " starts with world ? "+ boo2);

boolean boo3 = str1.startsWith("WORLD");
System.out.println("Does "+ str1 + " starts with WORLD ? "+ boo3);  
}
}


Output is :


String is Hello World
Does World Peace starts with World ? true
Does World Peace starts with world ? false
Does Hello World starts with WORLD ? false


Program Analysis





Advertisement




Another example of startsWith() method


// startsWith() method of String.

class StringStartsWith
{
public static void main(String[] ar)
{
String str1= new String("321... Let's go!!!");
System.out.println("String is "  + str1);

boolean boo1 = str1.startsWith("Let's");
System.out.println("Does String "+ str1 + " starts with Let's ? "+ boo1); 

boolean boo1 = str1.startsWith("21");
System.out.println("Does String "+ str1 + " starts with 21? "+ boo1); 

boolean boo2 = str1.startsWith("321");
System.out.println("Does String "+ str1 + " starts with 321 ? "+ boo2);

boolean boo3 = str1.startsWith("321..");
System.out.println("Does String "+ str1 + " starts with 321.. ? "+ boo3);  
}
}


Output is :


String is 321... Let's go!!!
Does String 321... Let's go!!! starts with Let's ? false
Does String 321... Let's go!!! starts with 21 ? false
Does String 321... Let's go!!! starts with 321 ? true
Does String 321... Let's go!!! starts with 321.. ? true


Program Analysis





Please share this article -




< Prev
Next >
< trim() method
endsWith() method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement