Just Split
the string on spaces and take the first element:
contactname.Split(" "c)(0)
Frage
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 :(
Lösung
Just Split
the string on spaces and take the first element:
contactname.Split(" "c)(0)
Andere Tipps
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