Is there a better way to replace strings?
I am surprised that Replace does not take in a character array or string array. I guess that I could write my own extension but I was curious if there is a better built in way to do the following? Notice the last Replace is a string not a character.
myString.Replace(';', '\n').Replace(',', '\n').Replace('\r', '\n').Replace('\t', '\n').Replace(' ', '\n').Replace("\n\n", "\n");
You can use a replace regular expression.
s/[;,\t\r ]|[\n]{2}/\n/g
s/
at the beginning means a search[
and ]
are the characters to search for (in any order)/
delimits the search-for text and the replace textIn English, this reads:
"Search for ;
or ,
or \t
or \r
or (space) or exactly two sequential
\n
and replace it with \n
"
In C#, you could do the following: (after importing System.Text.RegularExpressions
)
Regex pattern = new Regex("[;,\t\r ]|[\n]{2}");
pattern.Replace(myString, "\n");