Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I strip punctuation from a string?

Tags:

string

c#

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

like image 759
Tom Ritter Avatar asked Jan 07 '09 19:01

Tom Ritter


People also ask

How do you strip punctuation from a string 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.

How do you remove all spaces and punctuation from a 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.

How can I strip all punctuation from a string in JavaScript?

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.


1 Answers

new string(myCharCollection.Where(c => !char.IsPunctuation(c)).ToArray()); 
like image 62
GWLlosa Avatar answered Oct 23 '22 13:10

GWLlosa