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;
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With