LINQ query for string.replace

.net, c#, linq

Solution

For most string operations, you would be better off (in terms of both efficiency and conciseness) 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 preserve any Unicode alphanumeric character, including non-ASCII letters such as `é`, we can use the non-word character class to make it even simpler:

string input = "ASD@#$123";
string result = Regex.Replace(input, @"\W", "_");

For the sake of comparison, here is the same conversion done using LINQ (allowing just ASCII letters and digits):

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 allow non-ASCII letters, and is comparable to the `\w` word character class whose negation was used in our second example.

Edit: As Steve Wortham has observed, the LINQ versions are actually more than 3× faster than the regex (even when a `Regex` instance is created in advance with `RegexOptions.Compiled` and re-used).

Problem

Looking for help where given any string, return a string with alphanumeric characters only and replace all non-alphanumeric characters with _ so string "ASD@#$123" becomes "ASD___123" etc thanks

Original source

Related problems