c# - Removing special characters from strings in a List -
i have
list<string> names = new list<string>{"asa","@!","~!@#$%^tryt","asas**)_+lk"};//just example...will populated @ run time list<string> unsupportedcharacters = new list<string> { "~", "!", "#", "$", "%", "^", "&", "*"};
now want remove unsupported characters each string in "names" list. foreach loop , checking each string wondering if there better way of achieving this? may using linq ?
question edit
how if have replace unsupportedcharacters single space character..so "my@@naame!@%%is~~foo" should converted "my name foo"?ofcourse strings still in list "names"
edit 2 solved using regex.replace()
better way
. not sure. different way? maybe.
var names = new list<string> { "asa", "@!", "~!@#$%^tryt", "asas**)_+lk" }; var unsupportedcharacters = new hashset<char>("~!#$%^&*"); var newnames = names.select(n => string.join("", n.where(c => !unsupportedcharacters.contains(c)))) .tolist();
Comments
Post a Comment