How can I strip punctuation from a string?

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

    Here's a slightly different approach using linq. I like AviewAnew's but this avoids the Aggregate

            string myStr = "Hello there..';,]';';., Get rid of Punction";
    
            var s = from ch in myStr
                    where !Char.IsPunctuation(ch)
                    select ch;
    
            var bytes = UnicodeEncoding.ASCII.GetBytes(s.ToArray());
            var stringResult = UnicodeEncoding.ASCII.GetString(bytes);
    

提交回复
热议问题