Link to home
Start Free TrialLog in
Avatar of mousemat24
mousemat24

asked on

Check is firstname has 2 chars at the most, and a max of 50

Hi

I have a string called

string name = "fred"; // this is valid

string name = "h"; // this is an error

how to I check the name variable to see if there is at least 2 chars

thanks
Avatar of OklahomaDave
OklahomaDave

Try this...

if (str.Length<2){
    Console.WriteLine("invalid length");
}

Open in new window

string name = "fred"; // this is valid
string name = "h"; // this is an error

if (name.Length() <= 2)
{
// this is an error
}
else
{
// this is valid
}
Oops!

string name = "fred"; // this is valid
string name = "h"; // this is an error

if (name.Length() < 2)
{
// this is an error
}
else
{
// this is valid
}
ASKER CERTIFIED SOLUTION
Avatar of OklahomaDave
OklahomaDave

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mousemat24

ASKER

great, how about

must be greater then 2 and lower then 20?
string name = "fred"; // this is valid
string name = "h"; // this is an error

if (name.Length > 2 && name.Length < 20)
{
// this is valid
}
else
{
// this is an error
}