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 punctuation in any language?
I should add: Ideally, the solutions won't require you to enumerate all the possible punctuation marks.
Related: Strip Punctuation in Python
Use regex to Strip Punctuation From a String in Python The regex pattern [^\w\s] captures everything which is not a word or whitespace(i.e. the punctuations) and replaces it with an empty string.
Remove Punctuation From String Using the replaceAll() Method in Java. We can use a regex pattern in the replaceAll() method with the pattern as \\p{Punct} to remove all the punctuation from the string and get a string punctuation free. The regex pattern is \\p{Punct} , which means all the punctuation symbols.
We can use the JavaScript string replace method with a regex that matches the patterns in a string that we want to replace. So we can use it to remove punctuation by matching the punctuation and replacing them all with empty strings.
new string(myCharCollection.Where(c => !char.IsPunctuation(c)).ToArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With