سؤال

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