471,325 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

parsing a full name from a strong

hal
Hello,
I'm a beginner to C# programming and working on some exercises. I have
form where a user enters their name and when they click on a button it
parses their name and puts it on 3 seperate lines. Problem is I'm not
sure how to get this to work if the person only enters their first and
last name. I have it working fine if they enter their first, middle,
and last name. Can someone provide some sample code to get this to
work if the user only enters their first and last name or point me in
the right direction? Below is the code:

private void btnParseName_Click(object sender, System.EventArgs e)
{
string fullName = txtFullName.Text;
fullName = fullName.Trim();

string[] names = fullName.Split(' ');
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

Dec 21 '05 #1
7 6254
Hal,

You haven't thought of the other fun cases such as last names (and
sometimes middle or first names) that are not one word. And what about
honorifics and generation references? What do you do with Mr Le
Bloviate Frances De La Fubar IV?

You also aren't accounting for what happens if the user puts in two or
more consecutive spaces.

Trust me, you don't want to go here unless you are forced to. If you do
go there you need to devise some rules from a large database of names.

Components might be available to do this sort of thing too. The best
ones probably come with libraries of scrubbing routines for name/address
databases.

Best,

--Bob

hal wrote:
Hello,
I'm a beginner to C# programming and working on some exercises. I have
form where a user enters their name and when they click on a button it
parses their name and puts it on 3 seperate lines. Problem is I'm not
sure how to get this to work if the person only enters their first and
last name. I have it working fine if they enter their first, middle,
and last name. Can someone provide some sample code to get this to
work if the user only enters their first and last name or point me in
the right direction? Below is the code:

private void btnParseName_Click(object sender, System.EventArgs e)
{
string fullName = txtFullName.Text;
fullName = fullName.Trim();

string[] names = fullName.Split(' ');
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

Dec 21 '05 #2
hal
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

Dec 21 '05 #3
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");

--

Derek Davis
dd******@gmail.com

"hal" <bo******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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

Dec 21 '05 #4
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");
}

}

"hal" <bo******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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

Dec 21 '05 #5
hal
thanks for your help

Dec 21 '05 #6
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.

Dec 21 '05 #7
Bruce Wood <br*******@canada.com> wrote:
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...
}

--
Jon Skeet - <sk***@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
Dec 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Garyrek | last post: by
1 post views Thread by Alex | last post: by
3 posts views Thread by Aaron | last post: by
6 posts views Thread by raylopez99 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.