String methods – startsWith and endsWith

In this post, we will discuss 2 methods which are endsWith() and startsWith().

Let’s understand with examples.

boolean endsWith(String suffix):

This method checks if string ends with specified suffix. If ends with specified  suffix then returns true else false.

Output:
true
false

 

startswith() :

This method returns true if string starts with specified prefix, else will return false. It has 2 types of implementations.

1. boolean startsWith(String prefix): 

It checks if string starts with specified prefix or not.

Note: It will return true if the argument is an empty string.
Output:
true
false

boolean startsWith(String prefix,int toffset):

This method returns true if string starts with specified prefix starting from the given index.

Output:
true
false
Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
String methods - Concat and contains
String methods - contentEquals
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...