Replace the last occurrence of a word in a string – C#

Here is the function to replace the last occurrence of a string

public static string ReplaceLastOccurrence(string Source, string Find, string Replace)
{
    int place = Source.LastIndexOf(Find);
    
    if(place == -1)
       return Source;
    
    return Source.Remove(place, Find.Length).Insert(place, Replace);
}
  • Source is the string on which you want to do the operation.
  • Find is the string that you want to replace.
  • Replace is the string that you want to replace it with.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)