Pickup the first word in VB.NET

Hello Experts,

I have a solution how to  pickup the first word from MS SQL Database value, now I want to pickup the first 2-word in VB.NET from string values.


ACME Manufacturing Corp.. > ACME Manufacturing
Lords of the Ring > Lords of
King of the Jews > King of


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:

Try this,

Private Function ReturnFirstTwoWordsExample() As String
        Dim strVal As String = "ACME Manufacturing Corp.."
        Dim spaceCnt As Integer = 0
        Dim buffer As String = String.Empty
        Dim resultStr As String = String.Empty

        strVal = strVal.Trim
        For I As Integer = 0 To strVal.Length - 1
            If strVal(I) = " "c Then
                spaceCnt += 1
                If spaceCnt = 2 Then
                    Exit For
                End If
                resultStr = resultStr & strVal(I)
                resultStr = resultStr & strVal(I)
            End If
        Return resultStr
        'Result in "ACME Manufacturing"
    End Function

Open in new window

Try below code.

Hope this helps.
Dim YourData As String = ""
            Dim sString As String = ""
            sString = "ACME Manufacturing Corp.."
            Dim sSplit() As String            
            sSplit = sString.Split(" ")
            If sSplit.Length > 0 Then
                If sSplit.Length >= 2 Then
                    YourData = sSplit(0) & " " & sSplit(1)
                End If                
            End If

Open in new window

Carl TawnSystems and Integration DeveloperCommented:
Or you could use:
        Dim s As String = "Lord of the Rings"

        s = s.Substring(0, s.IndexOf(" "c, s.IndexOf(" "c) + 1))

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JaimeJegoniaAuthor Commented:
Carl Tawn,

I like your solution. It works!

JaimeJegoniaAuthor Commented:
Thank you guys for the great contribution.
All the samples worked but I like most Carl Tawn's solution - the lesser is the code is the best.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.