Jump to content
Xtreme .Net Talk

How to separate "Name Surnename" string into Name and Surname?


Recommended Posts

  • Leaders
Posted

use the Split function of the String Class , eg:

Dim NameSurname As String = "John Doe"
'/// use Split , the Space being the divider
Dim FirstName As String = NameSurname.Split(" ")(0)
Dim LastName As String = NameSurname.Split(" ")(1)

MessageBox.Show("the first name is " & FirstName & Environment.Newline & "the last name is " & LastName)

Posted

While dynamic_sysop's reply is correct, you may run into other troubles parsing free text names. For example, if the person's name is Oscar De LaHoya, or W. C. Fields - you get the idea.

 

I've been parsing free text names and addresses, or trying to, for years, and can say with confidence there's no fool-proof algorithm.

 

Not sure how fancy you need to get, but our current app gives its best attempt to parse, then displays the results to the user to see if it's ok and give a chance to correct.

Posted
While dynamic_sysop's reply is correct, you may run into other troubles parsing free text names. For example, if the person's name is Oscar De LaHoya, or W. C. Fields - you get the idea.

 

I've been parsing free text names and addresses, or trying to, for years, and can say with confidence there's no fool-proof algorithm.

True!

I tried it a couple of times too but then I told my manager that this wasn't polite and ever since I always use two fields for this. (Or three if the middle initials are important)

 

/Kejpa

Posted

// C# code

string name = "My really really long name";

int init;

while((init=name.IndexOf(" ",0))!=-1)

{

MessageBox.Show(name.Substring(0,init));

name = name.Remove(0,init);

name=name.Trim();

}

MessageBox.Show(name);

 

//should handle generic conditions where the names seperated by one or more spaces i guess

//its not an optimal solution though

//Insted of a messagebox an arraylist may be used to hold the names

 

Posted

While it's easy enought to break up a string given a certain delimiter, my point (which may not even be applicable) is that it is difficult, if not impossible, to parse names correctly. For example, the second token within a space-delimited string is not always the last name.

 

I agree with kejpa that one should seperate out the name parts, unless of course, you have no need to know the individual parts :p

 

Lately I've been using the Department of Justice's Global Justice XML Data Model for name parts. This goes much further that just first name, last name, and includes name prefix, suffix etc.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...