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
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
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).
Ö or α ,for ex, are alphanumeric chars :)char[] unwanted = new[] {'@', '#', '$'};
foreach(var x in query)
{
x.SomePropertyName = string.Join("_", x.SomePropertyName.Split(unwanted));
};
LINQ lambda expression to replace multiple characters in a string