• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

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
0
mousemat24
Asked:
mousemat24
  • 3
  • 2
1 Solution
 
OklahomaDaveCommented:
Try this...

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

Open in new window

0
 
Rajkumar GsSoftware EngineerCommented:
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
}
0
 
Rajkumar GsSoftware EngineerCommented:
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
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
OklahomaDaveCommented:
I noticed in your title you did not want the length to exceed 50 chars, so here's an addendum to that last snipped:

if ((str.length<2 || (str.length>50)){
   Console.WriteLine("Invalid length.");
}

Open in new window


-David
0
 
mousemat24Author Commented:
great, how about

must be greater then 2 and lower then 20?
0
 
Rajkumar GsSoftware EngineerCommented:
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
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now