Given a string and we have to find the last index of a substring in C#.
String.LastIndexOf()
String.LastIndexOf() Method returns trimmed string that will contain leading and trailing spaces.
Syntax:
int String.LastIndexOf(String str);
Example 1:
Input string is: "Hello there, how are you? Hello world."
Input substring (that we want to search) is: "Hello"
Output will be: 26 (because the index of last "Hello" is 26)
Example 2:
Input string is: "Hello there, how are you? Hello world."
Input substring (that we want to search) is: "Hi"
Output will be: Substring not found (here function will return negative value)
Consider the program:
Output