Solved

Parsing a name in vb.net

Posted on 2011-02-15
13
1,113 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
ID: 34897503
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
ID: 34897582
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
ID: 34897588
or this:

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

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:earngreen
ID: 34897684
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
ID: 34897686
I like HainKurt's solution.
0
 

Author Comment

by:earngreen
ID: 34897697
Dheist, this does not work if the initial is not there.
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34897841
"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
ID: 34897857
thanks twol :)
0
 

Author Comment

by:earngreen
ID: 34897963
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
ID: 34898029
 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
ID: 34898300
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
ID: 34898359
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
ID: 34899192
That worked. Thx
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get Client IP on RDS - VB.NET 15 44
VB.NET 1 21
cannot connect to sqlserver 8 25
Sql server insert 13 27
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

786 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