I have a list of strings. Some of them contain commas, single quotes, etc. Is there a quick way to replace those characters by blanks? My list looks something like this:
list = {"string1", "bla,bla", "more'stuff", "and more;stuff"}
cheers
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI have a list of strings. Some of them contain commas, single quotes, etc. Is there a quick way to replace those characters by blanks? My list looks something like this:
list = {"string1", "bla,bla", "more'stuff", "and more;stuff"}
cheers
Use
StringReplace[list, PunctuationCharacter -> " "]
(* {"string1", "bla bla", "more stuff", "and more stuff"} *)
PunctuationCharacter
is new in version 10.3. The 10.x updates received many new functions for text processing. In older versions I would use an explicit list of possible punctuation characters, i.e.
punctuation = Alternatives@@Characters[",.?:;'\"!-"]
I'm pretty sure I missed some.
StringReplace[list, punctuation -> " "]
(* {"string1", "bla bla", "more stuff", "and more stuff"} *)
StringReplace[list, RegularExpression["[[:punct:]]"] -> " "]
.
$\endgroup$
– J. M.'s ennui♦
Oct 27 '15 at 10:59