Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I locate the last occurrence of string in a string?

Tags:

.net

vb.net

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'.

like image 499
Cobold Avatar asked Aug 05 '11 12:08

Cobold


People also ask

How do you find the last instance of a character in a string?

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.

How can we check the last occuring index of a particular character 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.

How do I find the first and last occurrence of a character in a string?

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 .


1 Answers

string.LastIndexOf

like image 143
Sjoerd Avatar answered Sep 21 '22 23:09

Sjoerd