startsWith() String Example

startsWith() String Example


The String class comes with many helper methods with which Java string manipulation becomes easier. Forget the laborious days of C/C++. One such utility method is startsWith() String.

The present method startsWith(String str) checks the given string starts with a few characters like str or not. If starts with characters str, the method returns true else false.

startsWith() is overloaded 2 times. The signatures are given hereunder as defined in String class.

  1. public boolean startsWith(String str): Returns true if the given string starts with str, else false.
  2. public boolean startsWith(String str, int indexNumber): Returns true if the given string starts with str at index number indexNumber in the given string. That is, the previous method, checks at index number 0.

Following example illustrates the usage of both the above methods.


startsWith() String
Output screen on startsWith() String Example

A similar method exists in String class endsWith() checks in from the other end of the string.

Leave a Reply

Your email address will not be published. Required fields are marked *