【问题标题】:Remove Punctuation characters at last from a string最后从字符串中删除标点符号
【发布时间】:2014-02-04 06:57:42
【问题描述】:

我有一个字符串值和标点字符串。我需要从最后一个字符串值中删除标点符号,直到找到标点符号以外的字符。这是要删除的输入值和标点字符的示例字符串。

示例输入字符串

1. The Indian economy.My suggestion.,..,... ...

2. The Indian economy.,,[], ..My suggestion.,..,... ...[]

要删除的标点符号

[,.;:]

替换后的结果字符串

1. The Indian economy.My suggestion

2. The Indian economy.,,[], ..My suggestion

对此的任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: c# regex


    【解决方案1】:

    使用String.TrimEnd():

    string result=str.TrimEnd('.',',','[',']',' ');
    

    【讨论】:

      【解决方案2】:

      使用以下模式匹配尾随标点字符:

      @"\W+$"
      

      string str = @"1. The Indian economy.My suggestion.,..,... ...";
      string replaced = Regex.Replace(str, @"\W+$", "");
      Console.WriteLine(replaced); // => 1. The Indian economy.My suggestion
      

      \W 匹配非单词字符。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-08
        • 1970-01-01
        • 1970-01-01
        • 2014-12-06
        • 1970-01-01
        • 2015-07-07
        相关资源
        最近更新 更多