C# 程序来移除字符串的末尾部分
在 C# 中使用 Regex.Replace 方法来移除字符串的末尾部分。
以下面字符串为例 −
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!"; // replace the end part string s2 = System.Text.RegularExpressions.Regex.Replace(s1, "!", ""); Console.WriteLine("\"{0}\"
\"{1}\"", s1, s2); } } }
输出
"Demo Text!" "Demo Text"
广告