Solved

String Searching

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

816 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

10 Experts available now in Live!

Get 1:1 Help Now