문제

Hoping this would be simple, but doesnt seem to be.

I've got a variable in vb.net 'contactname'. format is like "John Smith"

I want to get just the forename from this, but cant seem to do it.

I've found and adapted some examples from google, but nothing seems to work :(

도움이 되었습니까?

해결책

Just Split the string on spaces and take the first element:

contactname.Split(" "c)(0)

다른 팁

Could use Regex if you like:

Public Shared Function RegexGetForename(ByVal str As String) As String
    Dim a = New System.Text.RegularExpressions.Regex("^(\w+)")
    If a.IsMatch(str) Then
        Return a.Match(str).Value
    Else
        Return vbNull
    End If
End Function
Dim forename as string
Dim i = contactname.IndexOf(" ")
            If i <> -1 Then
                forename = contactname.Substring(0, i)
                MsgBox(forename)

            End If

try it

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top