简体   繁体   中英

Check if string contains non-alphanumeric except underscore

I'm trying to write in if statement that runs if a string contains any non-alpahnumeric character with the exception of an underscore.

This is what I have and I'm trying to figure a simple way to add the exception for underscore but I'm having difficulty. (where key is a string).

// Check for non-alphanumerics except underscore
if (!(key.All(char.IsLetterOrDigit)))                                        
{
    validationResult = false;
}

您只需要扩展All内的逻辑:

if (!(key.All(c => char.IsLetterOrDigit(c) || c=='_'))) 

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