473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 6426
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Garyrek | last post by:
Hi I have an url where I have xml data POSTED and I need to parse the URL to read the data - Meanwhile here is my xml data =================================== <?xml version="1.0"...
1
by: Alex | last post by:
Hello, I don't have sufficient experience with XSLT, and would really appreciate somebody's help in me giving ideas on solving a problem I have. Let's consider the following XML file: ...
6
by: csinva | last post by:
I used some code from another post to successfully search and retrieve data from a web site. But now I have been pulling out my hair trying to take informaiton out of the file and display it in my...
4
by: Andreas Borutta | last post by:
Hi, what is the task with which I could not cope? There is a link with an inline-element (strong e.g.) inside. It has a higher font-size than the content of its parent. For the hover effect...
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
6
by: raylopez99 | last post by:
Anybody use Strong Name Signing? I think this is used by default for Resource files, which is one reason perhaps I can't get my resource files to work (somehow the public key is messed up, perhaps...
1
nine72
by: nine72 | last post by:
Ok, I am at a complete loss on this and have finally come to the XML Parsing Gods (and perhaps a PHP minor deity) for guidance… I will try my best to describe what I have going on… 1) I have 15...
5
by: jmurphy | last post by:
What do i add to this script to validate the entry? Thanks in advance for the help. <form name="continue" method="POST" action="shipping1.asp" onsubmit="return validate_form ();"> <input...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.