Wednesday 8 February 2017

Java String startsWith() Method



the startsWith() Method:

The startsWith() method is used to check if a given String begins with a specified String. The general form is:

boolean startswith(String str)

the code fragment

“Learning Java”.startsWith(“Java”);

It will return false.
You can also specify a string point to startsWith(). The general form is:

Boolean startsWith(String str, int startIndex);

Here startIndex specifies from where search will begin.

StartWithTest.java


 class StartsWithTest
 {
  public static void main(String arg[])
  {
   String str="Learning Java is fun";
   System.out.print("String str start with Learning:");
   System.out.println(str.startsWith("Learning"));
   System.out.print("String str start with Learning:");
   System.out.println(str.startsWith("Java"));
   System.out.print("String str start with 9th index:");
   System.out.println(str.startsWith("Java", 9));
  }
 }

The output is:

String str start with Learning:true
String str start with Learning:false
String str start with 9th index:true


0 comments:

Post a Comment

Powered by Blogger.

Stats