Thursday, April 19, 2018

Removing hidden/special characters from strings dot net

There will be situation when we need to remove special or hidden characters from a string. Below code will help you to chive the same.

C#

string output = new string(input.Where(c => !char.IsControl(c)).ToArray());
 
or 
 
string output = new string(input.Where(c => char.IsLetter(c) || char.IsDigit(c)).ToArray());
//if which to take only letters and

//if which to take only letters and digits
VB

Dim cleanString As String = Regex.Replace(yourString, "[^A-Za-z0-9\-/]", "")