Java String startsWith() method example.

Java 8Object Oriented ProgrammingProgramming

The startsWith(String prefix, int toffset) method of the String class tests if the substring of this string beginning at the specified index starts with the specified prefix.

Example

Live Demo
import java.lang.*;

public class StringDemo {
   public static void main(String[] args) {
      String str = "www.tutorialspoint.com";
      System.out.println(str);

      //The start string to be checked
      String startstr1 = "tutorialspoint";
      String startstr2 = "tutorialspoint";

      //Checks that string starts with given substring and starting index
      boolean retval1 = str.startsWith(startstr1);
      boolean retval2 = str.startsWith(startstr2, 4);

      //Prints true if the string starts with given substring
      System.out.println("starts with " + startstr1 + " ? " + retval1);
      System.out.println("string " + startstr2 + " starting from index 4 ? " + retval2);
   }
}

Output

www.tutorialspoint.com
starts with tutorialspoint ? false
string tutorialspoint starting from index 4 ? true
raja
Published on 12-Feb-2018 10:08:10
Advertisements