For most string operations, you would be better off (in terms of both efficiency and ) if you use regular expressions rather than LINQ:
string input = " ASD@ #$123"; string result = Regex.Replace(input, "[^A-Z0-9]", "_", RegexOptions.IgnoreCase);
If you want to store the Unicode alphanumeric character, including non-ASCII characters, such as é
, we can use the non-word character to make it even easier:
string input = " ASD@ #$123"; string result = Regex.Replace(input, @"\W", "_");
For comparison, this is the same conversion performed using LINQ (allowing ASCII letters and numbers):
string input = " ASD@ #$123"; string result = new string(input.Select(c => c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' ? c : '_' ).ToArray());
Or, if Char.IsLetterOrDigit
meets your requirements:
string input = " ASD@ #$123"; string result = new string(input.Select(c => char.IsLetterOrDigit(c) ? c : '_').ToArray());
Note that Char.IsLetterOrDigit
will contain non-ASCII letters and is comparable to the \w
character class of words, the negation of which was used in our second example.
Change As Steve Wortham noted, LINQ versions are actually more than 3 times faster than a regular expression (even if a Regex
instance is created in advance using RegexOptions.Compiled
used).
source share