【问题标题】:How to ask for user input with letters (a-z) only without special characters or numbers in C#如何在 C# 中仅使用字母 (a-z) 要求用户输入没有特殊字符或数字
【发布时间】:2016-03-24 01:26:20
【问题描述】:

我正在尝试创建一个程序,该程序要求我对用户输入进行验证,以仅接受给定数组 5 中的字母。因此,基本上,如果我是用户,则不允许输入数字或特殊字符,如果我这样做,我会得到一个错误。有人可以帮我解决这个问题吗?我已经尝试了各种搜索,但我无法找到解决方案。我很感激我能得到的任何帮助。 这是我目前所拥有的。

class Program
{
    static void Main(string[] args)
    {
        char[] arr = new char[5];

        //User input
        Console.WriteLine("Please Enter 5 Letters only: ");

        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = Convert.ToChar(Console.ReadLine());
        }
        //display
        for(int i = 0; i<arr.Length; i++)
        {
            Console.WriteLine("You have entered the following inputs: ");
            Console.WriteLine(arrArray[i]);
        }
    }
}

【问题讨论】:

标签: c#


【解决方案1】:
 char[] arr = new char[5];

            //User input
            Console.WriteLine("Please Enter 5 Letters only: ");
            string s = "abcdefghijklmnopqrstuvwxyz";
            for (int i = 0; i < arr.Length;)
            {
                string sChar = Console.ReadLine().ToLower();
                if (s.Contains(sChar) && sChar.Length == 1)
                {
                    arr[i] = Convert.ToChar(sChar);
                    i++;
                }
                else
                {
                    Console.WriteLine("Please enter a character from A-Z");
                    continue;
                }
            }
            //display
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("You have entered the following inputs: ");
                Console.WriteLine(arr[i]);
            }

【讨论】:

  • 感谢您的解决方案。这个解决方案非常有意义。我的头已经疼了 4 天了,现在想弄清楚这一点。再次感谢!
【解决方案2】:

我建议为此使用正则表达式(Regex)。

对于数字和字母,正确的正则表达式是:

string numbersLettersRegex = @"^[a-zA-Z0-9\_]+$"

然后,您只需检查该正则表达式:

if (Regex.isMatch(numbersLettersRegex, arr[i] 
{ 
    //do stuff
}

else 
{
    //print error Message
}

【讨论】:

  • 我一直在寻找很多使用正则表达式的解决方案,但这还不是我们所涵盖的内容,我对使用正则表达式的细节仍然很模糊。但是感谢您的帮助!
【解决方案3】:

将您尝试验证的字符串传递给此函数:

public bool checkYo(String myString)
{
    return Regex.IsMatch(myString, @"^[a-zA-Z]+$");
}

这将检查 a-z 和 A-Z.. 如果您只想要 a-z,只需从上述函数中删除 A-Z 部分。 希望这会有所帮助。

【讨论】:

    【解决方案4】:

    试试这个

    static void Main(string[] args) {

    char[] arr = new char[5];
    Console.WriteLine("Please Enter 5 Letters only: ");
    string inputstring = Console.ReadLine();
    
    if (Regex.IsMatch(inputstring, @"^[a-zA-Z]+$"))
    {
        arr = inputstring.ToCharArray();
        Console.WriteLine("You have entered the following inputs: ");
    
        //display
        for (int i = 0; i < arr.Length; i++)
        {
            Console.Write(arr[i]);
        }
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("Enter valid inputs and try again");
        Console.ReadLine();
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 2013-06-20
      • 2016-03-25
      • 1970-01-01
      相关资源
      最近更新 更多