How can I strip punctuation from a string?

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

    For long strings I use this:

    var normalized = input
                    .Where(c => !char.IsPunctuation(c))
                    .Aggregate(new StringBuilder(),
                               (current, next) => current.Append(next), sb => sb.ToString());
    

    performs much better than using string concatenations (though I agree it's less intuitive).

提交回复
热议问题