String - ReplaceLast
A string extension method that replace last occurence.
public static void Main() { string input = "aaabbbbccccaaadddddjjjjaaa"; // C# Extension Method: String - ReplaceLast string newStr = input.ReplaceLast("aaa", "zzz"); Console.WriteLine(newStr); // C# Extension Method: String - ReplaceLast newStr = input.ReplaceLast(2, "aaa", "zzz"); Console.WriteLine(newStr); }
View Source
using System; using System.Collections.Generic; using System.Linq; public static partial class Extensions { /// <summary> /// A string extension method that replace last occurence. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="oldValue">The old value.</param> /// <param name="newValue">The new value.</param> /// <returns>The string with the last occurence of old value replace by new value.</returns> public static string ReplaceLast(this string @this, string oldValue, string newValue) { int startindex = @this.LastIndexOf(oldValue); if (startindex == -1) { return @this; } return @this.Remove(startindex, oldValue.Length).Insert(startindex, newValue); } /// <summary> /// A string extension method that replace last numbers occurences. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="number">Number of.</param> /// <param name="oldValue">The old value.</param> /// <param name="newValue">The new value.</param> /// <returns>The string with the last numbers occurences of old value replace by new value.</returns> public static string ReplaceLast(this string @this, int number, string oldValue, string newValue) { List<string> list = @this.Split(oldValue).ToList(); int old = Math.Max(0, list.Count - number - 1); IEnumerable<string> listStart = list.Take(old); IEnumerable<string> listEnd = list.Skip(old); return string.Join(oldValue, listStart) + (old > 0 ? oldValue : "") + string.Join(newValue, listEnd); } }