What is LastIndexOf method and how it works?

Hello There,

Can someone please explain with a simple example of the “LastIndexOf” method? I somehow can’t see to understand this one …

Thanks in Advance
Hara

Hi @Hara_Gopal,

I assume this for string method:

" LastIndexOf () method is used to find index position of the last occurrence of a specified character or string within String.".

Please refer the below post:

Regards,
Sasikumar K

if the input string is “You searched for author Mark Twain. His books can be found in the following stores: Bookland,Classics bookstore.” and you want to get only the author i.e.“Mark Twain”,

You would be using one the expressions like,
message.Split("."c).First.ToString.Substring(message.LastIndexOf(“author”))

First part is,it splits by period symbol(.) then it takes only the FIRST part,then it find the substring in that part from the last till reached the word “author” as we mentioned there as a parameter.

8 Likes

hi @Hara_Gopal
“LastIndexof” will give you last occurrence of that string.
Example:- message=“aliabhatt”

so message.LastIndexof(“a”) will give —>6(bcoz last occurrence of “a” is at 6)

and message.indexof(“a”) will give—>0(first occurrence)

4 Likes

thank you so much

1 Like

thank you so much !