1

我有这样的方法:

public static bool IsValidNumberLetter { get; set; } 

 public static void IsNumLettersInput(string checkNumberLetter)
 {
  Validated.IsValidNumberLetter = checkNumberLetter.Any(Char.IsLetter) &
  checkNumberLetter.Any(Char.IsDigit) & (checkNumberLetter.Trim().Length == 12));
 }

这很好用,除了我想指定用户可以输入的字母数量:示例:1234QAZWSX。在这个例子中,我需要 X 个字母和 X 个数字,顺序无关紧要。

我不想使用正则表达式。

注意: Validated 是我从中调用变量IsValidNumberLetter的类的名称。这样,我可以在整个程序中使用变量来验证数字字母所需的任何内容。

使用上述方法可以吗?

感谢您的输入。

4

1 回答 1

3
public static bool IsValid(string source)
{
    //this will not affect the original as strings are immutable
    source = source.Trim(); 
    if (source.Length != 12) return false;
    return source.Take(4).All(char.IsLetter)
         && source.Skip(4).All(char.IsDigit);
}
于 2013-06-15T06:46:23.780 回答