Is there a function which would search for the last occurrence of a certain substring and return the index of it?
For example, if I want to search for the last occurrence of the char '\
' in a file path 'C:\Program Files\Microsoft
'.
The lastIndexOf() method returns the position of the last occurrence of specified character(s) in a string. Tip: Use the indexOf method to return the position of the first occurrence of specified character(s) in a string.
A simple solution to find the last index of a character in a string is using the rfind() function, which returns the index of the last occurrence in the string where the character is found and returns -1 otherwise.
The idea is to use charAt() method of String class to find the first and last character in a string. The charAt() method accepts a parameter as an index of the character to be returned. The first character in a string is present at index zero and the last character in a string is present at index length of string-1 .
string.LastIndexOf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With