Java.lang.String.startswith() in Java

There are two variants of startswith() method.This article depicts about all of them, as follows:
1. String startsWith() : This method tests if a string starts with the specified prefix beginning from 1st index.

Syntax
public boolean startsWith(String prefix)
Parameters
prefix: the prefix to be matched.
Return Value
It returns true if the character sequence 
represented by the argument is a prefix of the character 
sequence represented by this string; false otherwise.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the
// working of  startsWith()
public class Strt1 {
public static void main(String args[])
    {
  
        // Initialising String
        String Str = new String("Welcome to geeksforgeeks");
  
        // Testing the prefix using startsWith()
        System.out.print("Check whether string starts with Welcome : ");
        System.out.println(Str.startsWith("Welcome"));
  
        // Testing the prefix using startsWith()
        System.out.print("Check whether string starts with geeks : ");
        System.out.println(Str.startsWith("geeks"));
    }
}

chevron_right


Output:

Check whether string starts with Welcome : true
Check whether string starts with geeks : false

2. String startsWith(String prefix, int strt_pos):This variant has two argument and tests if a string starts with the specified prefix beginning a specified index.

Syntax
public boolean startsWith(String prefix, int strt_pos)
Parameters
prefix : the prefix to be matched.
strt_pos :  where to begin looking in the string.
Return Value
It returns true if the character sequence
represented by the argument is a prefix of the character
sequence represented by this string; false otherwise.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the
// working of  startsWith()
public class Strt2 {
public static void main(String args[])
    {
  
        // Initialising String
        String Str = new String("Welcome to geeksforgeeks");
  
        // Testing the prefix using startsWith()
        System.out.print("Check whether string starts with Welcome at pos 11 : ");
        System.out.println(Str.startsWith("Welcome", 11));
  
        // Testing the prefix using startsWith()
        System.out.print("Check whether string starts with geeks at pos 11 : ");
        System.out.println(Str.startsWith("geeks", 11));
    }
}

chevron_right


Output:

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

Possible applications : This method can primarily be useful to filter prefixes, for eg. filtering phone numbers starting from a number or names starting from a particular letter. Latter one is explained in this article.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the
// application of  startsWith()
public class Appli {
public static void main(String args[])
    {
  
        // Initialising String
        String Str = new String("Astha Tyagi");
  
        // Testing the prefix using startsWith()
        System.out.print("Check whether Astha Tyagi starts with A : ");
        System.out.println(Str.startsWith("A"));
    }
}

chevron_right


Output:

Check whether Astha Tyagi starts with A : true

This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Add Comment Collapse

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up