Check if string have uppercase, lowercase and number
How about?
if(myString.Any(char.IsLower) && myString.Any(char.IsUpper) && myString.Any(char.IsDigit))
You can use char
's methods with LINQ
:
if (myString.Any(char.IsUpper) &&
myString.Any(char.IsLower) &&
myString.Any(char.IsDigit))
For the sake of completess, the classic, non-LINQ way to achieve this:
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;
}
It is more efficient because it loops every character only once whereas the LINQ approaches need three enumerations.
if (myString.Any(ch => char.IsUpper(ch) &&
myString.Any(ch => char.IsLower(ch) &&
myString.Any(ch => char.IsDigit(ch))
{
this.hide();
}
else
{
MessageBox.Show("Error!");
}