earngreen
asked on
Parsing a name in vb.net
I am trying to parse the firstname, last name and middle initial in vb.net . I have a list of names that are formatted like the following. I just need to get the first and last name without the middle initial. Can anyone direct me how to handle?
Smith, Mary
Doe, Jane C.
Doe, John
Jack, Smith D.
Smith, Mary
Doe, Jane C.
Doe, John
Jack, Smith D.
dim name as string
if name.indexof(".") > 0 then
lasttname = (name.substring(0,name..in dexof(",") )
firstname = (name.substring(name..inde xof(",") + 2),(name.length - 2) - (name.substring(name..inde xof(",") + 2)))
else
lasttname = (name.substring(0,name..in dexof(",") )
firstname = (name.substring(name..inde xof(",") + 2),name.length - (name.substring(name..inde xof(",") + 2)))
endif
if name.indexof(".") > 0 then
lasttname = (name.substring(0,name..in
firstname = (name.substring(name..inde
else
lasttname = (name.substring(0,name..in
firstname = (name.substring(name..inde
endif
or this:
Dim name As String = "Jack, Smith D."
Dim names As String() = name.Split(" ")
Dim FirstNLast as String = names(0) & " " & names(1)
Dim name As String = "Jack, Smith D."
Dim names As String() = name.Split(" ")
Dim FirstNLast as String = names(0) & " " & names(1)
ASKER
hainkurt, I already use that and it does not trim the middle initial.
Dheist, I have dropped yours into a project but it trims the last letter of the first name. should it look more like this
Dim name As String
name = "Jack, Smith D."
Dim split() As String = name.Split(",")
Dim lastName As String = split(0)
Dim firstname As String = split(1)
firstname = firstname.Substring(1, firstname.Trim().IndexOf(" "))
Dheist, I have dropped yours into a project but it trims the last letter of the first name. should it look more like this
Dim name As String
name = "Jack, Smith D."
Dim split() As String = name.Split(",")
Dim lastName As String = split(0)
Dim firstname As String = split(1)
firstname = firstname.Substring(1, firstname.Trim().IndexOf("
I like HainKurt's solution.
ASKER
Dheist, this does not work if the initial is not there.
"I already use that and it does not trim the middle initial."
please give a sample showing why it is not working... for the samples you posted it should work fine, with orr without Initial...
'Dim name As String = "Jack, Smith D."
Dim name As String = "Doe, John"
Dim names As String() = name.Split(" ")
Dim FirstNLast as String = names(0) & " " & names(1)
please give a sample showing why it is not working... for the samples you posted it should work fine, with orr without Initial...
'Dim name As String = "Jack, Smith D."
Dim name As String = "Doe, John"
Dim names As String() = name.Split(" ")
Dim FirstNLast as String = names(0) & " " & names(1)
thanks twol :)
ASKER
I think we I am trying to accomplish is not clear.
If I have two names
Jack, Smith D. and Doe, John
I need to return just
Smith Jack and John Doe
If I have two names
Jack, Smith D. and Doe, John
I need to return just
Smith Jack and John Doe
ASKER
Dim name As String
name = "Jack, Smith D."
Would something like this work?
Dim split() As String = name.Split(",")
Dim lastName As String = split(0)
Dim firstname As String = split(1)
Dim Split2() As String = firstname.Split(" ")
Dim FirstNamewoInititial As String = Split2(1)
Label1.Text = FirstNamewoInititial & " " & lastName
name = "Jack, Smith D."
Would something like this work?
Dim split() As String = name.Split(",")
Dim lastName As String = split(0)
Dim firstname As String = split(1)
Dim Split2() As String = firstname.Split(" ")
Dim FirstNamewoInititial As String = Split2(1)
Label1.Text = FirstNamewoInititial & " " & lastName
or, for the first name, use my method,
start with the split as above, then
.....
Dim firstname As String = split(1)
if Firstname.indexof(".") > 0 then
firstname = firstname.substring(0,firs tname.leng th - 2)
endif
start with the split as above, then
.....
Dim firstname As String = split(1)
if Firstname.indexof(".") > 0 then
firstname = firstname.substring(0,firs
endif
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked. Thx
Open in new window