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.
xxxxxxxxxx
1
string text = "abc";
2
string to = "D";
3
4
string result = text.Substring(0, text.Length - 1) + to;
5
6
Console.WriteLine(result);
Output:
xxxxxxxxxx
1
abD
xxxxxxxxxx
1
string text = "abc";
2
string to = "D";
3
4
Regex expression = new Regex(".$");
5
string result = expression.Replace(text, to);
6
7
Console.WriteLine(result);
Output:
xxxxxxxxxx
1
abD
Note:
Duplicated article: C# / .NET - replace last char in string
In the future, combine these 2 articles into one.