String Searching

Posted on 2011-03-21
Last Modified: 2012-05-11
Hi, I have a string with a name in it: 'bob smith'. I want to parse the string and retrieve the first character of the second name, but not sure where to begin.

Any help would be appreciated,

Question by:SCOTT78
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
LVL 13

Expert Comment

ID: 35181740

You  can look for BOB and then thake the second name

        Dim strText As String = "asdfasdf asd bob smith asdfasdfas"
        Dim strSecondNameFirstLetter As String
        strSecondNameFirstLetter = strText.Substring(strText.IndexOf("bob") + 4, 1)
LVL 86

Accepted Solution

Mike Tomlinson earned 50 total points
ID: 35182060
You can use IndexOf() to find the space, then Substring() to grab the next character:
Dim str As String = "bob smith"
        Dim space As Integer = str.IndexOf(" ")
        If space <> -1 AndAlso (space + 1) < str.Length Then
            Dim FirstLetterOfSecondName As String = str.Substring(space + 1, 1)
        End If

Open in new window

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35182076
Another approach would be to Split() the string:
Dim str As String = "bob smith"
        Dim values() As String = str.Split()
        If values.GetUpperBound(0) >= 1 Then
            Dim FirstLetterOfSecondName As String = values(1).Substring(0, 1)
        End If

Open in new window

LVL 23

Expert Comment

ID: 35182686
Try this:

Dim name As String = "bob smith"
Console.WriteLine(Regex.Match(name, "(?<=\s)\S").Value)

Open in new window

LVL 27

Expert Comment

ID: 35183943
heres a good sample for you to play with:

  'strName =  " Title First MI Last + Credentials"
    Public Sub CheckForTitle(ByVal strName As String)
        Dim regExp As New Regex("([a-zA-Z]{1,})\.{1}\s{0,}([a-zA-Z]{1,})\s{0,}([a-zA-Z]{1,})\s{0,}([a-zA-Z]{1,})\s{0,}([a-zA-Z]\.)([a-zA-Z]\.)")
        Dim matchGroups As GroupCollection = regExp.Match(strName).Groups

        ' Title name
        ' First name
        ' Middle initial (If any)
        ' Last name
        '  Credentials
        MsgBox(matchGroups(5).Value + matchGroups(6).Value)
    End Sub

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

617 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