C# Program to remove the end part of a string


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

 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, "!", "");
         Console.WriteLine("\"{0}\"
\"{1}\"", s1, s2);       }    } }

Output

"Demo Text!"
"Demo Text"

Updated on: 22-Jun-2020

398 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements