C# string.replace to remove illegal characters
c#, string
Solution
Regular expressions are generally a good way to do that, but not when you're replacing every character with something different. You might consider replacing them all with the same thing, and just using `System.IO.Path.GetInvalidFileNameChars()`.
string filename = tVS.Nodes[r].Text;
foreach(char c in System.IO.Path.GetInvalidFileNameChars()) {
filename = filename.Replace(c, '_');
}
Problem
I'm working on a program that reads files and saves pieces of them according to their column's title. Some of those titles have illegal characters for file names, so i've written this piece of code to handle those issues. ``` string headerfile = saveDir + "\\" + tVS.Nodes[r].Text.Replace("\"", "").Replace ("/","").Replace(":"," -").Replace(">","(Greater Than)") + ".csv"; ``` Is there a nicer way of doing this where i don't have 4 `.Replace()`? or is there some sort of built in illegal character remover i don't know of? Thanks! EDIT: It does not need to replace the characters with anything specific. A blank space is sufficient.