Solved

Parsing a name in vb.net

Posted on 2011-02-15
13
1,114 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pop out of webbrowser1 control vba6 5 26
How to open form using item in Listbox. 8 23
Export PDF Form fields to Access  or Excel  in Tab order 16 80
Access Schema 6 26
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

830 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