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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Table and Column splitting 1

Status
Not open for further replies.

llmclaughlin

Programmer
Aug 20, 2004
140
US
I have a DataTable thats created from a file I read in. One of the columns in the table is for Name. When I read the table I'm creating a new file in a different file format. The name field in my table is formatted as Smith, John E. For the new file that I'm creating I need to break it out by last name, first name, and middle name. The questions is how can I do that. The below code is how I'm reading the table and the Name rows contains the full name.

With dsXTemp
For i = 0 To .Rows.Count - 1
sr.WriteLine(.Rows(i).Item("ID").ToString.PadRight(15) & _
.Rows(i).Item("Name").ToString.PadRight(25) & _
.Rows(i).Item("Name").ToString.PadRight(15) & _
.Rows(i).Item("Name").ToString.PadRight(1) & _
.Rows(i).Item("Cell").ToString.PadRight(20) & _
.Rows(i).Item("EndBal").ToString.PadRight(8))
Next
End With
So the first row name would be last name, second row name would be first name, thried row name is middle. But all name rows have full name Smith,John E

Thanks

Louie
 
Code:
'Split the name field by the ",". Anything before the "," is the last name
dim names() as string 
names() = dsXTemp.Rows(i).Item("Name").ToString.split(",")
LastName = names(0)

'Split what ever is left by the " ". The last element of the array is the middle name.
names() = names(1).split(" ")
MiddleName = names(names.length-1)

'Loop through what ever else is in the array and assemble it into a first name.
dim i as integer
for i = 0 to names.length - 2
  FirstName &= names(i) & " "
next i
FirstName = FirstName.trimend

You'll probably want to add some checks in there, in case the name field is poorly formated, or doesn't have a middle name or anything.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top