我需要为具有一些策略的控制台应用程序编写一个读取字符串方法

本文关键字:一个 方法 字符串 读取 应用程序 具有一 控制台 策略 | 更新日期: 2023-09-27 18:23:46

我试图应用的策略是它必须有3个字符长,必须包括小写字母、大写字母和任意顺序的数字。我试着用它作为一种方法来读取用户为分配设置的密码。我已经写了尽可能多的文章来消除错误,但我想不出一种方法来应用上述政策。这就是我目前所拥有的。。。如有任何帮助,将不胜感激

    private static string readPass() { //readpass method to read in password of 1 lower case, 1 upper case & 1 number

        bool validInput = true;
        char letterUserInput;                                           //letter from string
        int asciiCodeLetter;                                            //number code for letter
        string userInput;
        do {
            validInput = true;
            userInput = Console.ReadLine();
        try {
                if (userInput.Length < 1) {
                    validInput = false;
                    Console.WriteLine("You have just pressed Enter Please Enter a valid Password");
                }                      //if check length
                else if (userInput.Length > 1 && userInput.Length < 3) {
                    validInput = false;
                    Console.WriteLine("Password Is Too Short. Please Enter Valid Password");
                }                                                       //if check length too short
                else if (userInput.Length > 3)
                {
                    validInput = false;
                    Console.WriteLine("Password Is Too Long. Please Enter A Valid Password");
                }                                                       //if check length too short
                for (int i = 0; i < userInput.Length; i++)              //take each letter in turn from string
                {
                    letterUserInput = userInput[i];
                    asciiCodeLetter = Convert.ToInt16(letterUserInput);
                    if (asciiCodeLetter < 48 || asciiCodeLetter > 57)
                    {
                        validInput = false;
                        Console.WriteLine("You have used an invalid character.'n Password Must Contain 1 Upper Case, 1 Lower Case letter and 1 Number)");
                    }                                                    //character check with ASCII
                }
            }
            catch
            {
                validInput = false;
                Console.WriteLine("An unspecified error has occured");
            }                                                           //catch
            if (validInput == false) Console.Write("Invalid Password. Please Enter Again = ");
        } while (validInput == false);                                  // do. repeat entry if invalid
        return userInput;
    }

我需要为具有一些策略的控制台应用程序编写一个读取字符串方法

您可以为此使用正则表达式:

var input = Console.ReadLine();
var regex = new System.Text.RegularExpressions.Regex("^([0-9][a-z][A-Z]|[0-9][A-Z][a-z]|[a-z][0-9][A-Z]|[a-z][A-Z][0-9]|[A-Z][a-z][0-9]|[A-Z][0-9][a-z])$");
validInput = input != null && regex.IsMatch(input);

我相信Regex会更喜欢这个。但是,如果您不熟悉Regex表达式,这就足以理解它。

编辑

如果你想要更花哨的东西,你可以使用以下Regex:

 var input = Console.ReadLine();
 var regex = new System.Text.RegularExpressions.Regex(@"^(?=.*'d)(?=.*[a-z])(?=.*[A-Z]).{3}$");
 validInput = input != null && regex.IsMatch(input);

这方面的一些伪代码可能是:

String candidatePwd = textboxPwd.Text.Trim();
Boolean validPwd = IsValidPassword(candidatePwd);
private bool IsValidPassword(String candidate)
{
    return ((candidate.Length == 3) &&
            (ContainsOneUppercaseLetter(candidate)) &&
            (ContainsOneLowerCaseLetter(candidate)) &&
            (ContainsOneNumber(candidate)));
}
private bool ContainsOneUppercaseLetter(String candidate)
{
   // search for an uppercase letter; if one found, return true
}
private bool ContainsOneLowercaseLetter(String candidate)
{
   // search for a lowercase letter; if one found, return true
}
private bool ContainsOneUppercaseLetter(String candidate)
{
   // search for a number; if one found, return true
}

实际实现由您决定。

对于3个字符,只需将用户编写的密码保存为字符串,然后检查其长度。如果是大写,我会转换ASCII代码上的每个字符,然后检查是否有任何代码等于所有大写字母的代码。为了检查是否有数字,请使用类似的东西检查密码的每个字符

int n;bool isNumeric=int.TryParse("123",out n);

希望这会有所帮助。

您可以使用正则表达式:

^(?=.{3,})(?=.''d)(吗=.[a-z])(?=.[a-z]).*$

并简化您的代码:

if(!Regex.IsMatch(userInput, "^.*(?=.{7,})(?=.*'d)(?=.*[a-z])(?=.*[A-Z]).*$")
  Console.WriteLine("The password has to be 3 characters long, and must include a lower case letter, an upper case letter, and a number");