Java String startsWith example

String's startsWith method checks if String starts with specific prefix. It returns boolean datatype.

Method Signature:

public boolean startsWith(String prefix)

String startsWith Example:

package org.arpit.java2blog;

public class StringStartsWithExample {

 public static void main(String[] args) {
  String str = "java2blog";

  if (str.startsWith("java")) {
   System.out.println("String starts with java");
  } 
  else {
   System.out.println("String does not start with java");
  }

  if (str.startsWith("JAVA")) {
   System.out.println("String starts with JAVA");
  } 
  else {
   System.out.println("String does not start with JAVA");
  }
 }

}

When you run above program, you will get below output:
String starts with java
String does not start with JAVA

Please note that startsWith method is case sensitive as you can see in above example

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012