473,240 Members | 1,639 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,240 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 6413
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.