Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want one character to escape IsLetterOrDigit

Tags:

c#

asp.net

I have the following function that checks a string and keeps only the letters and digits. All other characters are removed.

However I want one character only "-" to escape this function and not to be removed. For instance I want Jean-Paul to stay as it with the "-" in between the two names. How can I do that?

String NameTextboxString = NameTextbox.Text;
NameTextboxString = new string((from c in NameTextboxString
                                where char.IsLetterOrDigit(c) 
                                select c).ToArray);
nameLabel.Text = NameTextboxString;
like image 974
Gloria Avatar asked Dec 09 '22 00:12

Gloria


1 Answers

You may try:

where char.IsLetterOrDigit(c) || c == '-'

Hoping that the user will not input string like --lolol-i-am-an-hackerz--


To simplify a bit your code:

nameLabel.Text = new string(NameTextbox.Text.Where((c => char.IsLetterOrDigit(c) || c == '-')).ToArray());
like image 107
Thomas Ayoub Avatar answered Dec 23 '22 18:12

Thomas Ayoub