我有一个输入字符串,我想验证它包含:

仅限信件或 只有字母和数字或者 只能使用字母、数字或下划线

为了澄清,我在代码中有3种不同的情况,每种情况调用不同的验证。用c#实现这个最简单的方法是什么?


当前回答

使用它扫描字符串的每个字符。

如果你想包含除“SPACE”之外的“许多”字符,你可以在1个IF语句后添加SWITCH语句。

string myInput = string.Empty;
bool check = false; 

// Loops the input module
while (check is false)
{
    Console.WriteLine("Enter Letters Only");
    myInput = Console.ReadLine();

    // Loops the SCANNING PROCCESS of each character of the string
    for (int i = 0; i < myInput.Length; i++)                  
    {
        // Prints current scanning proccess 1 by 1(character by character) inside the string
        Console.WriteLine("Checking Character \"{0}\" ",myInput[i]);
        
        // Letters only     
        if (Char.IsLetter(myInput[i]))
        {
            check = true;
        }
        
        // Includes "SPACE" character
        else if (myInput[i] == ' ')
        {
            check = true;
        }
        else
        {
            check = false;
            Console.WriteLine("wrong input. \"{0}\" is not a string", myInput[e]);
            Console.WriteLine("pls try again");
            // Exits from loop of scanning proccess due to unwanted input
            break;        
        }  
    }
    // Ends SCANNING of 1 CHARACTER inside the string
} 
Console.WriteLine("Input Approved: \"{0}\"", myInput);
Console.WriteLine("Press any key to exit");
Console.ReadKey();

输出:错误输入

只输入字母 代理47 检查字符“A” 检查字符“g” 检查字符“e” 检查字符“n” 检查字符“t” 检查字符" " 检查字符“4” 哦。“4”不是有效输入 请再试一次 只输入字母

输出:无错误输入

只输入字母 特工47 检查字符“A” 检查字符“g” 检查字符“e” 检查字符“n” 检查字符“t” 检查字符" " 检查字符“F” 检查字符“o” 检查字符“r” 检查字符“t” 检查字符“y” 检查字符" " 检查字符“S” 检查字符“e” 检查字符“v” 检查字符“e” 检查字符“n” 批准输入:“47号特工” 按任意键退出

或SWITCH语句,如果你只想包括“一些”字符从字母类别和“一些”字符从数字类别。

switch (myInput[e])
{ 
    case 'a':
    case 'b':
    case 'c':
    case 'd': 
    case '1':
    case '2':
    case '3':
    case '!':
    case '@': 
    case '#':  
        check = true;
        break; 
    default:
        check = false;
        Console.WriteLine("Oops, \"{0}\" is not a string", myInput[i]);
        Console.WriteLine("pls try again\n");
        break;
}
if (check == false) break ;

其他回答

最近,我在这个页面的帮助下对一个检查字符串中的字母的函数进行了性能改进。

我发现使用regex的解比使用Char的解慢30倍。IsLetterOrDigit检查。

我们不确定这些字母或数字包括,我们只需要拉丁字符,所以实现了基于反编译版本的Char的功能。IsLetterOrDigit函数。

下面是我们的解决方案:

internal static bool CheckAllowedChars(char uc)
    {
        switch (uc)
        {
            case '-':
            case '.':
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
            case 'G':
            case 'H':
            case 'I':
            case 'J':
            case 'K':
            case 'L':
            case 'M':
            case 'N':
            case 'O':
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
            case 'V':
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                return true;
            default:
                return false;
        }
    }

用法是这样的:

 if( logicalId.All(c => CheckAllowedChars(c)))
 { // Do your stuff here.. }

迭代通过字符串字符和使用'Char'函数称为'IsLetter'和'IsDigit'。

如果你需要更具体的东西-使用Regex类。

只有字母:

Regex.IsMatch(input, @"^[a-zA-Z]+$");

仅限字母和数字:

Regex.IsMatch(input, @"^[a-zA-Z0-9]+$");

只有字母、数字和下划线:

Regex.IsMatch(input, @"^[a-zA-Z0-9_]+$");

使用它扫描字符串的每个字符。

如果你想包含除“SPACE”之外的“许多”字符,你可以在1个IF语句后添加SWITCH语句。

string myInput = string.Empty;
bool check = false; 

// Loops the input module
while (check is false)
{
    Console.WriteLine("Enter Letters Only");
    myInput = Console.ReadLine();

    // Loops the SCANNING PROCCESS of each character of the string
    for (int i = 0; i < myInput.Length; i++)                  
    {
        // Prints current scanning proccess 1 by 1(character by character) inside the string
        Console.WriteLine("Checking Character \"{0}\" ",myInput[i]);
        
        // Letters only     
        if (Char.IsLetter(myInput[i]))
        {
            check = true;
        }
        
        // Includes "SPACE" character
        else if (myInput[i] == ' ')
        {
            check = true;
        }
        else
        {
            check = false;
            Console.WriteLine("wrong input. \"{0}\" is not a string", myInput[e]);
            Console.WriteLine("pls try again");
            // Exits from loop of scanning proccess due to unwanted input
            break;        
        }  
    }
    // Ends SCANNING of 1 CHARACTER inside the string
} 
Console.WriteLine("Input Approved: \"{0}\"", myInput);
Console.WriteLine("Press any key to exit");
Console.ReadKey();

输出:错误输入

只输入字母 代理47 检查字符“A” 检查字符“g” 检查字符“e” 检查字符“n” 检查字符“t” 检查字符" " 检查字符“4” 哦。“4”不是有效输入 请再试一次 只输入字母

输出:无错误输入

只输入字母 特工47 检查字符“A” 检查字符“g” 检查字符“e” 检查字符“n” 检查字符“t” 检查字符" " 检查字符“F” 检查字符“o” 检查字符“r” 检查字符“t” 检查字符“y” 检查字符" " 检查字符“S” 检查字符“e” 检查字符“v” 检查字符“e” 检查字符“n” 批准输入:“47号特工” 按任意键退出

或SWITCH语句,如果你只想包括“一些”字符从字母类别和“一些”字符从数字类别。

switch (myInput[e])
{ 
    case 'a':
    case 'b':
    case 'c':
    case 'd': 
    case '1':
    case '2':
    case '3':
    case '!':
    case '@': 
    case '#':  
        check = true;
        break; 
    default:
        check = false;
        Console.WriteLine("Oops, \"{0}\" is not a string", myInput[i]);
        Console.WriteLine("pls try again\n");
        break;
}
if (check == false) break ;
bool result = input.All(Char.IsLetter);

bool result = input.All(Char.IsLetterOrDigit);

bool result = input.All(c=>Char.IsLetterOrDigit(c) || c=='_');