String methods – isEmpty() and lastIndexof

In this post, we will discuss about 2 methods which are isEmpty and lastIndexOf.

Let’s understand them with examples.

boolean isEmpty():

Returns true , if length of the string is 0.


lastIndexOf():

This method finds out the index of last occurrence of a char/sub-string in a particular String.

It has 4 types of implementations.

int lastIndexOf(int ch): It returns the last occurrence of character ch in the particular String.

int lastIndexOf(int ch, int fromIndex): It returns the last occurrence of ch, starting searching backward from the specified index “fromIndex”.

int lastIndexOf(String str): Returns the last occurrence of str in a String.

int lastIndexOf(String str, int fromIndex): Returns the last occurrence of str, starting searching backward from the specified index “fromIndex”.

Output: Last occurence of string COM :12
Last occurence of char C :12
Last occurence of char G, index start from 10 :6
Last occurence of string Pool,index starts from 10 :7
Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Print Friendly, PDF & Email
String methods - toString,toUppercase,toLowercase,toChararray
String methods - replace
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...