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"

更新日期: 22-6 月-2020

473 次浏览

启动您的 职业

完成课程获取证书

开始
广告