Java.lang.String.startsWith() Method

Advertisements


Description

The java.lang.String.startsWith(String prefix) method tests if this string starts with the specified prefix.

Declaration

Following is the declaration for java.lang.String.startsWith() method

public boolean startsWith(String prefix)

Parameters

  • prefix -- This is the value of prefix.

Return Value

This method returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string, else false.

Exception

  • NA

Example

The following example shows the usage of java.lang.String.startsWith() method.

package com.tutorialspoint;

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 = "www";
    String startstr2 = "http://";

    // checks that string starts with given substring
    boolean retval1 = str.startsWith(startstr1);
    boolean retval2 = str.startsWith(startstr2);

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

Let us compile and run the above program, this will produce the following result:

www.tutorialspoint.com
starts with www ? true
starts with http:// ? false



Advertisements
Advertisements