Solved

Parsing a name in vb.net

Posted on 2011-02-15
13
1,108 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:earngreen
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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

0
 
LVL 7

Expert Comment

by:twol
Comment Utility
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

0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
or this:

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

0
 

Author Comment

by:earngreen
Comment Utility
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(" "))
0
 
LVL 7

Expert Comment

by:twol
Comment Utility
I like HainKurt's solution.
0
 

Author Comment

by:earngreen
Comment Utility
Dheist, this does not work if the initial is not there.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
"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)
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
thanks twol :)
0
 

Author Comment

by:earngreen
Comment Utility
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




0
 

Author Comment

by:earngreen
Comment Utility
 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
0
 
LVL 7

Expert Comment

by:twol
Comment Utility
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
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
Comment Utility
try this :)

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

Author Closing Comment

by:earngreen
Comment Utility
That worked. Thx
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now