Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Parsing a name in vb.net

Posted on 2011-02-15
13
Medium Priority
?
1,127 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 59

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 59

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 59

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 59

Accepted Solution

by:
HainKurt earned 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…
Suggested Courses

670 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