Java Data Type Tutorial - Java String.startsWith(String prefix)








Syntax

String.startsWith(String prefix) has the following syntax.

public boolean startsWith(String prefix)

Example

In the following code shows how to use String.startsWith(String prefix) method.

//from   w  ww . j  a  v  a  2s  .com

public class Main {

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

    String startstr1 = "www";
    String startstr2 = "http://";

    boolean retval1 = str.startsWith(startstr1);
    boolean retval2 = str.startsWith(startstr2);

    System.out.println(retval1);
    System.out.println(retval2);
  }
}

The code above generates the following result.