How can I strip punctuation from a string?

前端 未结 15 585
天命终不由人
天命终不由人 2020-12-04 18:47

For the hope-to-have-an-answer-in-30-seconds part of this question, I\'m specifically looking for C#

But in the general case, what\'s the best way to strip punctuati

15条回答
  •  忘掉有多难
    2020-12-04 19:30

    You can use the regex.replace method:

     replace(YourString, RegularExpressionWithPunctuationMarks, Empty String)
    

    Since this returns a string, your method will look something like this:

     string s = Regex.Replace("Hello!?!?!?!", "[?!]", "");
    

    You can replace "[?!]" with something more sophiticated if you want:

    (\p{P})
    

    This should find any punctuation.

提交回复
热议问题