Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split Full Name into Three Separate Items

Status
Not open for further replies.

vbnetnewby1967

Technical User
Aug 16, 2005
7
US
Hello...
I have a problem that I am hoping someone can help me with.

I have a field which contains the full name of an individual (i.e. "Public, John Q").

Is there a way to split this single field into three separate items that I can use to populate other variables.

I've used Split to get the last name and first name, but with the first name I end up with a firstname and middle initial. I need the middle initial separate as well.

Does anyone have a simple solution for this problem?
Thanks in advance.
 
Not reliably.

People from Spain/Portugal tend to have multiple middle names, and doing a simple Split() will get it wrong.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
I found this someplace else and seems to work...
Dim FullName As String
FullName = "Public, John Q"

UseRegularExpression(FullName)

End Sub


Private Sub UseRegularExpression(ByVal strName As String)
Dim regExp As New Regex("([a-zA-Z]{1,})\,{1}\s{0,}([a-zA-Z]{1,})\s{0,}([a-zA-Z]{0,})")
Dim matchGroups As GroupCollection = regExp.Match(strName).Groups

' Last name
MsgBox(matchGroups(1).Value)
' First name
MsgBox(matchGroups(2).Value)
' Middle initial (If any)
MsgBox(matchGroups(3).Value)
End Sub
 
How would that work with:

surname:
O'Neill
du Maurier
van Beethoven
or hyphenated

?

Additionally firstnames can be hyphenated or contain apostrophes and there may be more than one middle initial.

Your regular expression allows for an alpha surname of one or more characters a required comma, zero or more spaces followed by an alpha firstname of one or more characters followed by zero or more spaces and ending with zero or more alpha characters (no spaces allowed between middle initials if they exist)

As chip said, its not as easy as it may at first seem. It can be done (to a great extent) with a regular expression, but it would need to be far more comprehensive than the one you posted.

Sorry to put a dampener on your solution.


[vampire][bat]
 
Code:
dim FullName as String =  "Public, John Q"
dim FirstName as string = FullName.Split(",")(1).Split(" ")(0)

dim i as integer
dim MiddleName as string 
for 1 = 1 to FullName.Split(",")(1).Split(" ").length-1
  MiddleName &= FullName.Split(",")(1).Split(" ")(0)
next

dim LastName as string = FullName.Split(",")(0)

It might take some tweeking, but it should be close. Provided you always have the format <LastName>,<FirstName> <MiddleName>. Lastname is always followed by a comma, firstname has no spaces but can be hyfinated, and middle name can be as long as you like with what every characters.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I haven't had a chance to experiment with this yet, but I was thinking along the lines of using MeasureString to work out where in the TextBox the cursor is and then add this to the .Top and .Left of the TextBox, thus giving the .Top and .Left of the pop-up form. I'll hopefully have a chance to play with this tonight.

[vampire][bat]
 
Is that just a default reply you give to everyone now E&F? [wink]


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top