Link to home
Start Free TrialLog in
Avatar of earngreen
earngreenFlag for United States of America

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.
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

A very easy method is using string-methods
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(0, firstname.Trim().IndexOf(" "))

Open in new window

dim name as string

if name.indexof(".") > 0 then
   lasttname = (name.substring(0,name..indexof(",") )
   firstname =  (name.substring(name..indexof(",") + 2),(name.length - 2) - (name.substring(name..indexof(",") + 2)))
else
   lasttname = (name.substring(0,name..indexof(",") )
   firstname =  (name.substring(name..indexof(",") + 2),name.length - (name.substring(name..indexof(",") + 2)))
endif

or this:

Dim name As String = "Jack, Smith D."
Dim names As String() = name.Split(" ")
Dim FirstNLast as String = names(0) & " " & names(1)

Avatar of earngreen

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(" "))
I like HainKurt's solution.
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)
thanks twol :)
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




 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
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,firstname.length - 2)
endif
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked. Thx