Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查字符串是否有大小写和数字_C#_String_If Statement - Fatal编程技术网

C# 检查字符串是否有大小写和数字

C# 检查字符串是否有大小写和数字,c#,string,if-statement,C#,String,If Statement,我想检查我的字符串是否有大小写和数字 string myString = "Hello123"; if (myString haveUppercase && myString haveLowerCase && myString haveNumber) { this.hide(); } else { MessageBox.Show("Error!"); } 您可以将char的方法与LINQ一起使用: if (myString.Any(char.I

我想检查我的字符串是否有大小写和数字

string myString = "Hello123";

if (myString haveUppercase && myString haveLowerCase && myString haveNumber)
{
    this.hide();
}
else
{
    MessageBox.Show("Error!");
}

您可以将
char
的方法与
LINQ
一起使用:

if (myString.Any(char.IsUpper) &&
    myString.Any(char.IsLower) &&
    myString.Any(char.IsDigit))
怎么样

if(myString.Any(char.IsLower) && myString.Any(char.IsUpper) && myString.Any(char.IsDigit))

为了实现这一点,completess是一种经典的非LINQ方法:

public static bool HasUpperLowerDigit(string text)
{
    bool hasUpper = false; bool hasLower = false; bool hasDigit = false;
    for (int i = 0; i < text.Length && !(hasUpper && hasLower && hasDigit); i++)
    {
        char c = text[i];
        if (!hasUpper) hasUpper = char.IsUpper(c);
        if (!hasLower) hasLower = char.IsLower(c);
        if (!hasDigit) hasDigit = char.IsDigit(c);
    }
    return hasUpper && hasLower && hasDigit;
}
公共静态bool hassupperLowerDigit(字符串文本)
{
bool hasUpper=false;bool hasLower=false;bool hasDigit=false;
对于(int i=0;i

它更有效,因为它只循环每个字符一次,而LINQ方法需要三个枚举。

我们可以对字母数字组使用扩展方法,如下所示

(我们也可以对小写字母、大写字母使用不同的扩展方法。) 字母和数字分开(如果需要)

我们可以使用上述扩展方法,如下所示:

 {

 string strValue = "vasanth";

 strValue.IsAlphaNumeric();  //return true

 string strValue1 = "vasanth!";

 strValue.IsAlphaNumeric();  //return false

  }
#区域检查是否有大写字母

        string Mystring = "SimpleWordforExAmple";
        char[] chars;
        char ch;
        int length = Mystring.Length;
        int cnt;
        int totalcntupper = 0;

        chars = Mystring.ToCharArray(0, length);

        Console.WriteLine("Sample words with capital letters : {0} ", Mystring);
        for (cnt = 0; cnt < length;cnt ++)
        {
            ch = chars[cnt];


            if (char.IsUpper(ch))
            {
                Console.WriteLine("Capital letter : #{0}", ch);
                totalcntupper++;

            }


        }

        Console.WriteLine("Count of capital letter(s) : # {0}", totalcntupper);
        Console.ReadLine();








        #endregion
string Mystring=“simplewordexample”;
字符[]字符;
char ch;
int length=Mystring.length;
int-cnt;
int totalcntupper=0;
chars=Mystring.ToCharArray(0,长度);
WriteLine(“带大写字母的示例单词:{0}”,Mystring);
对于(cnt=0;cnt

我希望你有个主意。谢谢

看起来您正在尝试进行一些强大的密码验证。 虽然有很多方法可以剥除这只猫的皮肤,但我更喜欢将这些代码包装到可重用的扩展方法中,这样以后就可以轻松地完成。使用扩展方法时,还可以避免使用正则表达式,因为它比直接字符检查慢。我喜欢使用extensions.csnuget包中的扩展。它使该检查变得非常简单:

string myString = "Hello123";

//if (myString haveUppercase && myString haveLowerCase && myString haveNumber)
if (myString.IsStrong(true, true, true, false))
//If you wanted to allow special characters and require 3 of the 4 it would simply be:
if (myString.IsStrong(3))
{
    this.hide();
}
else
{
    MessageBox.Show("Error!");
}
  • 将包添加到项目中
  • 在代码顶部添加“
    使用扩展;
  • 您的示例代码将变得非常简单:

    string myString = "Hello123";
    
    //if (myString haveUppercase && myString haveLowerCase && myString haveNumber)
    if (myString.IsStrong(true, true, true, false))
    //If you wanted to allow special characters and require 3 of the 4 it would simply be:
    if (myString.IsStrong(3))
    {
        this.hide();
    }
    else
    {
        MessageBox.Show("Error!");
    }
    

    某些语言(例如汉语)不区分大小写,因为它们是表意文字,如果这对您的软件很重要的话
    string myString = "Hello123";
    
    //if (myString haveUppercase && myString haveLowerCase && myString haveNumber)
    if (myString.IsStrong(true, true, true, false))
    //If you wanted to allow special characters and require 3 of the 4 it would simply be:
    if (myString.IsStrong(3))
    {
        this.hide();
    }
    else
    {
        MessageBox.Show("Error!");
    }