Java String startsWith() Method



The Java String startsWith() method is used to check if the string begins with the specified prefix. The prefix which needs to be verified is provided to the function in the form of a string. A single character may also serve as the prefix.

This method retrieves the boolean value true if the string contains the specified prefix; otherwise false. For example, for the string “TutorialsPoint” if you invoke this method by passing “Tut” as an argument then, it will return the boolean value true.

This method has two polymorphic variants, the syntax of which is shown below.

Syntax

Following is the syntax for Java String startsWith() method −

public boolean startsWith(String prefix) // first syntax
or,
public boolean startsWith(String prefix, int toffset) // second syntax

Parameters

  • prefix − This is the value of prefix.

  • toffset − This is where to begin looking in this string. // second syntax

Return Value

  • The public boolean startsWith(String prefix) method returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string, else false.

  • The public boolean startsWith(String prefix, int toffset) method returns true if the character sequence represented by the argument is a prefix of the substring of this object starting at index toffset, else false.

Example

The following example shows the usage of Java String startsWith() method. Here we are initializing a String with an URL and verifying whether it starts with "www" −

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";
      
      // checks that string starts with given substring
      boolean retval1 = str.startsWith(startstr1);
      
      // prints true if the string starts with given substring
      System.out.println("starts with " + startstr1 + " ? " + retval1);
   }
}

Output

If you compile and run the above program, it will produce the following result −

www.tutorialspoint.com
starts with www ? true

Example

Following is another example of this function −

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 = "com";
      
      // checks that string starts with given substring
      boolean retval1 = str.startsWith(startstr1);
      
      // prints true if the string starts with given substring otherwise false
      System.out.println("starts with " + startstr1 + " ? " + retval1);
   }
}

Output

If you compile and run the program above, the output will be displayed as follows −

www.tutorialspoint.com
starts with com ? false

Example

The following example shows the usage of Java String startsWith() method by initializing a String object and starting the match by providing the index. Here it returns a failed match −

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";
      
      // checks that string starts with given substring and starting index
      boolean retval1 = str.startsWith(startstr1);
      
      // prints true if the string starts with given substring
      System.out.println("starts with " + startstr1 + " ? " + retval1);
   }
}

Output

On executing the program above, the output is obtained as follows −

www.tutorialspoint.com
starts with tutorialspoint ? false

Example

A String object is initialized with the value "www.tutorialspoint.com" and we are invoking the startsWith() method on this string by passing the toffset value along with the prefix string −

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";
      
      // checks that string starts with given substring and starting index
      boolean retval1 = str.startsWith(startstr1, 4);
      // prints true if the string starts with given substring otherwise false
      System.out.println("string " + startstr1 + " starting from index 4 ? " + retval1);
   }
}

Output

The output of the above program is as follows −

www.tutorialspoint.com
string tutorialspoint starting from index 4 ? true

Example

The NullPointerException is thrown if we pass null as an argument. The reason for this is that null cannot be used as an argument.

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String s = "Welcome to tutorials Point";
      System.out.println("The string is: " + s.startsWith(null));
   }
}

NullPointerException

While executing the above code we get the following output −

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "prefix" is null
      at java.base/java.lang.String.startsWith(String.java:2253)
      at java.base/java.lang.String.startsWith(String.java:2296)
      at StringDemo.main(StringDemo.java:6)
java_lang_string.htm
Advertisements