public static String ReplaceLast(string input, string oldValue, string newValue) { int index = input.LastIndexOf(oldValue); if (index < 0) { return input; } else { StringBuilder sb = new StringBuilder(input.Length - oldValue.Length + newValue.Length); sb.Append(input.Substring(0, index)); sb.Append(newValue); sb.Append(input.Substring(index + oldValue.Length, input.Length - index - oldValue.Length)); return sb.ToString(); } }