By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

How to separate names into first last and middle initial

P: 2
Im trying to Separate Last name, First Name and Middle Initial. File is csv format, here is an example:

Expand|Select|Wrap|Line Numbers
  1. A0001,3,Y,13,LU, 
  2. A0001,3,Y,13,CLARK P, 
  3. A0001,3,Y,13,SMITH JOHN, 
  4. A0001,3,Y,13,BEAL KRISS J, 
  5. A0001,3,Y,13,THOMAS A CLIFF C, 
  6. A0001,3,Y,13,DEW III ROBERT H, 
Output fields :
Expand|Select|Wrap|Line Numbers
  1.                Last name               First name           Initial    
  2.                   LU                     
  3.                   CLARK                   P  
  4.                   SMITH                   JOHN  
  5.                   BEAL                    KRISS                J  
  6.                   THOMAS A                CLIFF                C  
  7.                   DEW III                 ROBERT               H 
Thanks In Advance.
Sep 24 '10 #1
Share this Question
Share on Google+
2 Replies

Aimee Bailey
Expert 100+
P: 197
Normally all you need to do is read split up the data by line breaks, then by the comma. Heres a simple function that can do that:

Expand|Select|Wrap|Line Numbers
  1. Public Function ReadCSV(ByVal data As String) As List(Of String())
  2.         Dim result As New List(Of String())
  3.         Dim lines As String() = data.Split(vbCrLf.ToString())
  5.         For Each line As String In lines
  6.             result.Add(line.Split(New Char() {","}, _
  7.                                   StringSplitOptions.RemoveEmptyEntries))
  8.         Next
  10.         Return result
  11.     End Function
And to use it:

Expand|Select|Wrap|Line Numbers
  1. Dim data As String = "A0001,3,Y,13,LU, " & vbCrLf & _
  2.                           "A0001,3,Y,13,CLARK P, " & vbCrLf & _
  3.                           "A0001,3,Y,13,SMITH JOHN, " & vbCrLf & _
  4.                           "A0001,3,Y,13,BEAL KRISS J, " & vbCrLf & _
  5.                           "A0001,3,Y,13,THOMAS A CLIFF C, " & vbCrLf & _
  6.                           "A0001,3,Y,13,DEW III ROBERT H, "
  8.         For Each row In ReadCSV(data)
  9.             ListBox1.Items.Add(row(4))
  10.         Next
Sep 26 '10 #2

P: 296
Seems to me that you've been asked to fix broken data. If the firstname, lastname and middle initial were not entered in specific fields for their respective purposes - there is no clean way to retrieve it that way.
You will inevitably run into examples like
John Smith III
Smith John III
Smith III John

So unless you are going to read each entry and clean up the data - then you will likely wind up with some percentage of incorrectly mapped fields. If there's not a lot of data - fine, do it by hand. If there is, yikes.
Sep 26 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.