String startswith() Method in Java with Examples
startWith() method of String class 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
Variants Of Methods
There are two variants of the startswith() method that are as follows:
- startsWith(String prefix, int strt_pos)
Variant 1: String startsWith()
This method tests if a string starts with the specified prefix beginning from the first index.
public boolean startsWith(String prefix)
Parameters: The prefix to be matched.
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, else returns false.
Variant 2: String startsWith(String prefix, int strt_pos)
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
public boolean startsWith(String prefix, int strt_pos)
- 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.
Check whether string starts with Welcome at pos 11 : false Check whether string starts with geeks at pos 11 : true
Now let us look up possible applications seen in daily life. This method can primarily be useful to filter prefixes. For example: filtering phone numbers starting from a number of names starting from a particular letter. The latter one is explained in this article.
Check Whether It Starts With Letter 'S' : true
This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Please Login to comment...