EN
C# / .NET - replace last character in string
3
points
In C#/.NET it is possible to replace last character in string in few ways.
1. string.Substring
example
string text = "abc";
string to = "D";
string result = text.Substring(0, text.Length - 1) + to;
Console.WriteLine(result);
Output:
abD
2. Regex.Replace
example
string text = "abc";
string to = "D";
Regex expression = new Regex(".$");
string result = expression.Replace(text, to);
Console.WriteLine(result);
Output:
abD
Note:
Duplicated article: C# / .NET - replace last char in string
In the future, combine these 2 articles into one.