C# Program to remove the end part of a string

CsharpProgrammingServer Side Programming

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 −


 Live Demo

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, "!", "");
\"{1}\"", s1, s2);       }    } }


"Demo Text!"
"Demo Text"
Updated on 22-Jun-2020 14:53:18