joshua magsino joshua magsino - 1 year ago 120 Question

getting a substring of a string in

I have this kind of string

Lastname, FirstName MiddleName

I would like them to be put in 3 different textboxes


but i cant seem to get it right,

I have these code for my project:

Name = Me.dtgEmplist.CurrentRow.Cells(1).Value

fname = Name.Substring(Name.IndexOf(" ")).Trim
mname = fname.Substring(fname.IndexOf(" ")).Trim
lname = Name.Substring(0, Name.IndexOf(",")).Trim

I seem to get the LastName and middlenames correctly, but the firstname is very tricky, I only succeed in getting this kind of output

FirstName MiddleName

could someone help me out? I've been trying this out for so long.

EDIT: they are taken from a datagridview. And the datagridview view is populated from a stored procedure

Answer Source

It should be easy if you use Split method as the others said:

Name = Me.dtgEmplist.CurrentRow.Cells(1).Value

Dim FirstSplit as String()
FirstSplit = Name.Split(",")
fname = FirstSplit(0).Trim()

Dim SecondSplit as String() = (FirstSplit(1).Trim()).Split()

mname = SecondSplit(0).Trim
lname = SecondSplit(1).Trim

First of all you split for the ',' character and then you split for the space. Maybe you also should check for array dimension.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download