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 −

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

Output

"Demo Text!"
"Demo Text"
raja
Published on 05-Sep-2018 11:33:30
Advertisements