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


String Searching

Posted on 2011-03-21
Medium Priority
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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

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