• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

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.

Examples:

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

Thanks.


0
JaimeJegonia
Asked:
JaimeJegonia
3 Solutions
 
Alfred A.Commented:
Hi,

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)
            Else
                resultStr = resultStr & strVal(I)
            End If
        Next
        Return resultStr
        'Result in "ACME Manufacturing"
    End Function

Open in new window

0
 
darshan_derasariCommented:
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

0
 
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

0
 
JaimeJegoniaAuthor Commented:
Carl Tawn,

I like your solution. It works!


Thanks.
0
 
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now