Method # 1: Using
This method returns the last occurrence of a substring if present in a string. The disadvantage of this function is that it throws an exception if there is no substring in the string, and therefore breaks the code.
The original string: GfG is best for CS a nd also best for Learning Index of last occurrence of substring is: 28
Method # 2: Using
rfind() p >
This is an alternative method to accomplish this task. The advantage that this function offers better than the above method is that this function returns “-1” if the substring is not found, instead of throwing an error.
Output: b >
The original string: GfG is best for CS and also best for Learning Index of last occurrence of substring is: 28