remove special characters from string
Is there a build in function that removes the following without doing a replace for each character?
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"
Re: remove special characters from string
You've got no choice but to do each one, but there are various ways to do it. This would be the most succinct option:
vb.net Code:
Dim builder As New StringBuilder("NULaSTXbSOHcSTX")
Dim substrings = {"NUL", "SOH", "STX"}
Array.ForEach(substrings, Sub(s) builder.Replace(s, String.Empty))
MessageBox.Show(builder.ToString())
Using a StringBuilder makes it much more efficient than a String for many replacements.
Re: remove special characters from string
Use regular expressions.
Code:
Imports System.Text.RegularExpressions
TargetString = Regex.Replace(TargetString, "NUL|SOH|STX|ETX", "")
Just enter all the substrings as I showed with |'s separating them. Each of them will be replaced with nothing.
This will help you build additional regular expressions: http://www.regular-expressions.info/reference.html