简体   繁体   中英

LINQ query for string.replace

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).

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

Here is the function for you:

    String ReplaceWrongChars(String baseString)
    {
        Regex rx = new Regex("[^A-Za-z0-9 ]", RegexOptions.CultureInvariant);
        String rv = rx.Replace(baseString, "_");

        return rv;
    }

If you do not need spaces included, use "[^A-Za-z0-9]" as regular expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM