String startsWith(String prefix, int strt_pos) method

This variant has two arguments and tests if a string starts with the specified prefix beginning a specified index. Here we are passing a starting position to be matched which allows us the flexibility  

Syntax

public boolean startsWith(String prefix, int strt_pos)

Parameters:

  • prefix: The prefix is to be matched.
  • strt_pos: Start position where to begin looking in the string.

Return Type: A boolean value that returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise.

Example:

Java




// Java Program to Demonstrate Working of startsWith()
// Method of String Class
 
// Importing required classes
import java.lang.String;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Declaring and initialising custom string
        String Str = new String("Welcome to w3wiki");
 
        // Display message
        System.out.print(
            "Check whether string starts with Welcome at pos 11 : ");
        // Testing the prefix using startsWith() method
        System.out.println(Str.startsWith("Welcome", 11));
 
        // Display message
        System.out.print(
            "Check whether string starts with geeks at pos 11 : ");
        // Testing the prefix using startsWith() method
        System.out.println(Str.startsWith("Geeks", 11));
    }
}


Output

Check whether string starts with Welcome at pos 11 : false
Check whether string starts with geeks at pos 11 : true

String startswith() Method in Java with Examples

In Java, startWith() method of the String class is present in java.lang package is used to check whether the string starts with a specific prefix. The package view is as follows:

 --> java.lang Package
    --> String Class
         --> startWith() Method   

Similar Reads

Variants Of String startsWith() method

There are two variants of the startswith() method that are as follows:...

1. String startsWith() method

This method tests if a string starts with the specified prefix beginning from the first index....

2. String startsWith(String prefix, int strt_pos) method

...

Real-Life Example

This variant has two arguments and tests if a string starts with the specified prefix beginning a specified index. Here we are passing a starting position to be matched which allows us the flexibility...