How can I strip punctuation from a string?

前端 未结 15 572
天命终不由人
天命终不由人 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 19:19

    Based off GWLlosa's idea, I was able to come up with the supremely ugly, but working:

    string s = "cat!";
    s = s.ToCharArray().ToList()
          .Where(x => !char.IsPunctuation(x))
          .Aggregate(string.Empty, new Func(
                 delegate(string s, char c) { return s + c; }));
    

提交回复
热议问题