检查字符串是否只包含字母、数字和下划线

时间:2022-09-11 18:30:52

I have to check if a string contains only letters, digits and underscores. This is how I tried but it doesn't work:

我必须检查一个字符串是否只包含字母、数字和下划线。我就是这样尝试的,但是没有效果:

for(int i = 0; i<=snameA.Length-1; i++)
{
    validA = validA && (char.IsLetterOrDigit(snameA[i])||snameA[i].Equals("_"));
}

6 个解决方案

#1


8  

I love Linq for this kind of question:

我喜欢问Linq这样的问题:

bool validA = sname.All(c => Char.IsLetterOrDigit(c) || c.Equals('_'));

#2


10  

You are assigning validA every time again, without checking its previous value. Now you always get the value of the last check executed.

每次都要分配validA,而不检查它的前一个值。现在,您总是会得到执行最后一次检查的值。

You could 'and' the result:

你可以'和'结果:

validA &= (char.IsLetterOrDigit(snameA[i]) || snameA[i] == '_');

This would mean you still run all characters, which might be useless if the first check failed. So it is better to simply step out if it fails:

这意味着您仍然运行所有字符,如果第一次检查失败,那么这些字符可能是无用的。因此,如果失败了,最好直接退出:

for(int i = 0; i<=snameA.Length-1; i++)
{
    validA = (char.IsLetterOrDigit(snameA[i]) || snameA[i] == '_');

    if (!validA)
    { break; } // <-- see here
}

Or with LINQ:

或用LINQ:

validA = snameA.All(c => char.IsLetterOrDigit(c) || c == '_');

#3


4  

you can use regex

您可以使用正则表达式

Regex regex1 = new Regex(@"^[a-zA-Z0-9_]+$");

if(regex1.IsMatch(snameA))
{

}

#4


3  

You could try matching a regular expression. There is a built in type for "letters, digits and underscores", which is "\w".

您可以尝试匹配正则表达式。有一个内置的“字母、数字和下划线”类型,即“\w”。

Regex rgx = new Regex(@"\w*");
rgs.IsMatch(yourString);

If you require 1 or more, then use "\w+".

如果您需要1个或更多,请使用“\w+”。

Further information here: Regex.IsMatch

进一步的信息:Regex.IsMatch

#5


2  

I would use a Regex

我会用正则表达式

string pattern = @"^[a-zA-Z0-9\_]+$";
Regex regex = new Regex(pattern);

// Compare a string against the regular expression
return regex.IsMatch(stringToTest);

#6


1  

First, letter is a bit vague term: do you mean a..z and A..Z characters or letter could belong to any alphabet, e.g. а..я and А..Я (Russian, Cyrillic letters). According to your current implementation, you want the second option.

首先,字母是一个模糊的词:你是说…z和. .Z字母表可以属于任何字符或字母,例如,а。я和А. .Я(俄罗斯,西里尔字母)。根据当前实现,需要第二个选项。

Typical solution with loop is to check until first counter example:

循环的典型解决方案是检查直到第一个反例:

  Boolean validA = true; // true - no counter examples so far

  // Why for? foreach is much readble here
  foreach(Char ch in sname) 
    // "!= '_'" is more readable than "Equals"; and wants no boxing
    if (!char.IsLetterOrDigit(ch) && ! (ch != '_')) { 
      Boolean validA = false; // counter example (i.e. non-letter/digit symbol found)

      break; // <- do not forget this: there's no use to check other characters
    }

However you can simplify the code with either Linq:

但是,您可以使用Linq来简化代码:

  validA = sname.All(ch => Char.IsLetterOrDigit(ch) || ch == '_');

Or regular expression:

或正则表达式:

  validA = Regex.IsMatch(sname, @"^\w*$");

#1


8  

I love Linq for this kind of question:

我喜欢问Linq这样的问题:

bool validA = sname.All(c => Char.IsLetterOrDigit(c) || c.Equals('_'));

#2


10  

You are assigning validA every time again, without checking its previous value. Now you always get the value of the last check executed.

每次都要分配validA,而不检查它的前一个值。现在,您总是会得到执行最后一次检查的值。

You could 'and' the result:

你可以'和'结果:

validA &= (char.IsLetterOrDigit(snameA[i]) || snameA[i] == '_');

This would mean you still run all characters, which might be useless if the first check failed. So it is better to simply step out if it fails:

这意味着您仍然运行所有字符,如果第一次检查失败,那么这些字符可能是无用的。因此,如果失败了,最好直接退出:

for(int i = 0; i<=snameA.Length-1; i++)
{
    validA = (char.IsLetterOrDigit(snameA[i]) || snameA[i] == '_');

    if (!validA)
    { break; } // <-- see here
}

Or with LINQ:

或用LINQ:

validA = snameA.All(c => char.IsLetterOrDigit(c) || c == '_');

#3


4  

you can use regex

您可以使用正则表达式

Regex regex1 = new Regex(@"^[a-zA-Z0-9_]+$");

if(regex1.IsMatch(snameA))
{

}

#4


3  

You could try matching a regular expression. There is a built in type for "letters, digits and underscores", which is "\w".

您可以尝试匹配正则表达式。有一个内置的“字母、数字和下划线”类型,即“\w”。

Regex rgx = new Regex(@"\w*");
rgs.IsMatch(yourString);

If you require 1 or more, then use "\w+".

如果您需要1个或更多,请使用“\w+”。

Further information here: Regex.IsMatch

进一步的信息:Regex.IsMatch

#5


2  

I would use a Regex

我会用正则表达式

string pattern = @"^[a-zA-Z0-9\_]+$";
Regex regex = new Regex(pattern);

// Compare a string against the regular expression
return regex.IsMatch(stringToTest);

#6


1  

First, letter is a bit vague term: do you mean a..z and A..Z characters or letter could belong to any alphabet, e.g. а..я and А..Я (Russian, Cyrillic letters). According to your current implementation, you want the second option.

首先,字母是一个模糊的词:你是说…z和. .Z字母表可以属于任何字符或字母,例如,а。я和А. .Я(俄罗斯,西里尔字母)。根据当前实现,需要第二个选项。

Typical solution with loop is to check until first counter example:

循环的典型解决方案是检查直到第一个反例:

  Boolean validA = true; // true - no counter examples so far

  // Why for? foreach is much readble here
  foreach(Char ch in sname) 
    // "!= '_'" is more readable than "Equals"; and wants no boxing
    if (!char.IsLetterOrDigit(ch) && ! (ch != '_')) { 
      Boolean validA = false; // counter example (i.e. non-letter/digit symbol found)

      break; // <- do not forget this: there's no use to check other characters
    }

However you can simplify the code with either Linq:

但是,您可以使用Linq来简化代码:

  validA = sname.All(ch => Char.IsLetterOrDigit(ch) || ch == '_');

Or regular expression:

或正则表达式:

  validA = Regex.IsMatch(sname, @"^\w*$");