How can I strip punctuation from a string?

前端 未结 15 570
天命终不由人
天命终不由人 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:15

    Assuming "best" means "simplest" I suggest using something like this:

    String stripped = input.replaceAll("\\p{Punct}+", "");
    

    This example is for Java, but all sufficiently modern Regex engines should support this (or something similar).

    Edit: the Unicode-Aware version would be this:

    String stripped = input.replaceAll("\\p{P}+", "");
    

    The first version only looks at punctuation characters contained in ASCII.

提交回复
热议问题