简体   繁体   English

有没有办法找出C#中的字符串是否不包含数字和字符0-9和AZ?

[英]Is there a way I can find out if a string in C# does not contain the numbers & characters 0-9 and A-Z?

我已经看过某种正则表达式测试,但是如何检查字符串是否不包含任何0-9数字或AZ字符?

You don't need a regex: 您不需要正则表达式:

string s = ...
bool hasLetterOrDigit = s.Any(c => char.IsLetterOrDigit(c));

bool onlyLetterOrDigit = s.All(c => char.IsLetterOrDigit(c));

The IsLetterOrDigit() function uses the Unicode classification ( MSDN ). IsLetterOrDigit()函数使用Unicode分类( MSDN )。

If you want precise control: 如果要精确控制:

string s = ...
string allowedChars = "abcdefABCDEF0123456789";

bool onlyAllowedChar = s.All(c => allowedChars.Contains(c));

使用正则表达式尝试

bool Exists=Regex.IsMatch(input, @"[^A-Z0-9]+");

something like... 就像是...

if ((Regex.IsMatch(value, "[^A-Z0-9]+")
{
   //do something
}

Edit: Changed to match Matt's suggestion. 编辑:更改为匹配Matt的建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C# 中将字符串从数字 0-9 增加到小写 az 到大写 AZ - Increment a string from numbers 0-9 to lowercase a-z to uppercase A-Z in C# 如何创建az和0-9(最多8个字符)的字典并将其另存为.txt文件? - How can I create a dictionary with a-z and 0-9 up to 8 characters long and save it as a .txt file? 如何提取第一个字符和之后的数字,直到在字符串中找到字符 (az) - C# 2.0 - How to extract first character and numbers after that until find a character (a-z) in a string - C# 2.0 如何获取正则表达式来检查字符串是否只包含字母字符[az]或[AZ]? - How can I get a regex to check that a string only contains alpha characters [a-z] or [A-Z]? 我有正则表达式将字符串拆分为单词,数字和标点符号列表。 如何制作列表中的“az”和“0-9”单个元素? - I have regex to split string to words, numbers and punctuation marks list. How to make “a-z” and “0-9” single elements of list? 如何在C#中没有特殊字符或数字的情况下用字母(az)询问用户输入 - How to ask for user input with letters (a-z) only without special characters or numbers in C# 如何匹配两个括号之间的这种模式(字符 az,数字)? - How can I match this pattern (characters a-z, numbers ) between two brackets? C#Regex:检查“az”和“AZ” - C# Regex: Checking for “a-z” and “A-Z” C#是否可以识别以0-9以外的数字开头的数字? - Does C# recognize numbers beginning with digits outside 0-9? 正则表达式:0-9 AZ az或其中任何一个'!#$%&'* + - / =?^ _`{|}〜 - Regular Expression: 0-9 A-Z a-z or any of these '!#$%&'*+-/=?^_`{|}~
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM