Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

parsing a full name from a strong

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hal - 21 Dec 2005 21:33 GMT
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
Bob Grommes - 21 Dec 2005 21:58 GMT
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

> Hello,
> I'm a beginner to C# programming and working on some exercises.  I have
[quoted text clipped - 22 lines]
>
> Thanks
hal - 21 Dec 2005 22:04 GMT
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
thanks for your help
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.