Share on Google+Share on Google+


String class method - startsWith()


String.startsWith() is again a String Class method of Java that checks if the given string starts with the prefix char or not.

It take parameters..
prefix - the prefix of the string.

Syntax of the String.startsWith() method

public boolean startsWith(String prefix)

a Simple Example:

public static void main(String[] args)
String str1 = "Awards for Small Businesse.";
String str2 = "Small business market.";

String startStr = "Sm";

boolean starts1 = str1.startsWith(startStr);
boolean starts2 = str2.startsWith(startStr);

System.out.println("\"" + str1 + "\" starts with " +
"\"" + startStr + "\"? " + starts1);
System.out.println("\"" + str2 + "\" starts with " +
"\"" + startStr + "\"? " + starts2);

the Output is:

"Awards for Small Businesse." starts with "Sm"? false
"Small business market." starts with "Sm"? true




Posted on: March 17, 2005 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: java.lang.String.startsWith()  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image