C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: We combine it with the "$" character at the end, which signals the end of the string.
Step 1: We declare a new string that contains an ending substring we need to remove. This is what we will use Regex upon.
Step 2: Here we invoke the Regex.Replace static method. We pass 3 arguments to Regex.Replace.
Step 3: We display the resulting string copy, which has been stripped of the ending string.
ConsoleC# program that removes string
using System;
class Program
{
static void Main()
{
// 1
// The string you want to change.
string s1 = "This string has something at the end<br/>";
// 2
// Use regular expression to trim the ending string.
string s2 = System.Text.RegularExpressions.Regex.Replace(s1,
"<br/>$", "");
// 3
// Display the results.
Console.WriteLine("\"{0}\"\n\"{1}\"",
s1,
s2);
}
}
Output
"This string has something at the end<br/>"
"This string has something at the end"
Note: This means that the pattern won't be replaced if it occurs anywhere else in the string.