Solved

String Searching

Posted on 2011-03-21
5
259 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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