Solved

String Searching

Posted on 2011-03-21
5
256 Views
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,

Thanks
0
Comment
Question by:SCOTT78
5 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35181740
Hi,

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)
        MsgBox(strSecondNameFirstLetter)
0
 
LVL 85

Accepted Solution

by:
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)
            MessageBox.Show(FirstLetterOfSecondName)
        End If

Open in new window

0
 
LVL 85

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)
            MessageBox.Show(FirstLetterOfSecondName)
        End If

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35182686
Try this:

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

Open in new window

0
 
LVL 27

Expert Comment

by:planocz
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
        MsgBox(matchGroups(1).Value)
        ' First name
        MsgBox(matchGroups(2).Value)
        ' Middle initial (If any)
        MsgBox(matchGroups(3).Value)
        ' Last name
        MsgBox(matchGroups(4).Value)
        '  Credentials
        MsgBox(matchGroups(5).Value + matchGroups(6).Value)
    End Sub
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now