C# 程序删除字符串的结尾部分
csharpprogrammingserver side programming更新于 2025/9/16 6:47:00
使用 Regex.Replace 方法在 C# 中删除字符串的结尾部分。
以下是字符串 −
string s1 = "Demo Text!";
现在,假设您需要从字符串中删除感叹号 (!)。为此,只需使用 replace − 将其设置为空即可。
System.Text.RegularExpressions.Regex.Replace(s1, "!", "");
这是完整的代码 −
示例
using System;
using System.Text.RegularExpressions;
namespace Demo {
class Program {
static void Main(string[] args) {
string s1 = "Demo Text!";
// 替换结尾部分
string s2 = System.Text.RegularExpressions.Regex.Replace(s1, "!", "");
Console.WriteLine("\"{0}\"
\"{1}\"", s1, s2);
}
}
}
输出
"Demo Text!" "Demo Text"

