Use the Regex.Replace method to remove the end part of a string in C#.
The following is the string −
string s1 = "Demo Text!";
Now, let us say you need to remove the exclamation mark (!) from the string. For that just set it to empty using replace −
System.Text.RegularExpressions.Regex.Replace(s1, "!", "");
Here is the complete code −
Example
using System;
using System.Text.RegularExpressions;
namespace Demo {
class Program {
static void Main(string[] args) {
string s1 = "Demo Text!";
// replace the end part
string s2 = System.Text.RegularExpressions.Regex.Replace(s1, "!", "");
Console.WriteLine("\"{0}\"\n\"{1}\"", s1, s2);
}
}
}Output
"Demo Text!" "Demo Text"