Thanks for your response. I understand the potential problems when
doing this way. I'm just following the directions for this exercise.
For my purposes lets assume the user will either enter a first and last
name or a first, middle, and last name. They will not enter just a
first or last name or any generation references. Also assuming these
users are perfect and will not put more than one space inbetween names
and they always put their first name first and last name last.
Assuming all this how would I get the message box only to display the
first and last names if the user doesn't enter a middle name?
Thanks
carion1 - 21 Dec 2005 22:57 GMT
string thisIsNotGood = "";
string fullName = txtFullName.Text;
fullName = fullName.Trim();
string[] names = fullName.Split(' ');
if(names.GetUpperBound(0) == 1)
{
thisIsNotGood += "First Name:\t" + names[0] + "\n\n";
thisIsNotGood += "Last Name:\t" + names[1] + "\n\n";
}
else if(names.GetUpperBound(0) == 2)
{
thisIsNotGood += "First Name:\t" + names[0] + "\n\n";
thisIsNotGood += "Middle Name:\t" + names[1] + "\n\n";
thisIsNotGood += "Last Name:\t" + names[2] + "\n";
}
else
{
thisIsNotGood += "Your name was not in the expected format!";
}
MessageBox.Show(thisIsNotGood, "Parse Name");

Signature
Derek Davis
ddavis76@gmail.com
> Thanks for your response. I understand the potential problems when
> doing this way. I'm just following the directions for this exercise.
[quoted text clipped - 7 lines]
>
> Thanks
hal - 21 Dec 2005 23:20 GMT
Mel Weaver - 21 Dec 2005 23:07 GMT
In a very simple form and using your assumptions
private void btnParseName_Click(object sender, System.EventArgs e)
{
string fullName = txtFullName.Text;
fullName = fullName.Trim();
string[] names = fullName.Split(' ');
if (names.Length == 2)
{
string firstName = names[0];
string lastName = names[1];
MessageBox.Show("First Name:\t" + firstName + "\n\n" +
"Last Name:\t" + lastName + "\n",
"Parse Name");
}
else
{
string firstName = names[0];
string middleName = names[1];
string lastName = names[2];
MessageBox.Show("First Name:\t" + firstName + "\n\n" +
"Middle Name:\t" + middleName + "\n\n" +
"Last Name:\t" + lastName + "\n", "Parse Name");
}
}
> Thanks for your response. I understand the potential problems when
> doing this way. I'm just following the directions for this exercise.
[quoted text clipped - 7 lines]
>
> Thanks
Bruce Wood - 21 Dec 2005 23:21 GMT
I would make a minor change:
private void btnParseName_Click(object sender, System.EventArgs e)
{
string fullName = txtFullName.Text;
fullName = fullName.Trim();
string[] names = fullName.Split(' ');
string firstName;
string middleName;
string lastName;
if (names.Length == 2)
{
firstName = names[0];
middleName = "";
lastName = names[1];
MessageBox.Show("First Name:\t" + firstName + "\n\n" +
"Last Name:\t" + lastName +
"\n",
"Parse Name");
}
else
{
firstName = names[0];
middleName = names[1];
lastName = names[2];
MessageBox.Show("First Name:\t" + firstName + "\n\n" +
"Middle Name:\t" + middleName + "\n\n" +
"Last Name:\t" + lastName + "\n", "Parse Name");
}
}
Not really a big deal, but at least firstName, middleName and lastName
are now available outside the "if" statement if you want to do
something with them.
Jon Skeet [C# MVP] - 22 Dec 2005 06:46 GMT
> I would make a minor change:
Me too:
private void btnParseName_Click(object sender, System.EventArgs e)
{
string fullName = txtFullName.Text.Trim();
string[] bits = fullName.Split(' ');
string firstName = bits[0];
string lastName = bits[bits.Length-1];
string middleName = (bits.Length==2 ? bits[1] : "");
// Do whatever you want with the bits...
}

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too