Breaking apart full name into first and last name in VB.net RRS feed

  • Question

  • I have an Input box that allows users to enter their full name.  How do I break this input apart into first and last name to display tham into seperate boxes?
    Thursday, March 17, 2011 9:04 PM


  • Something like the following should show you how to break a string into its components parts where it is splitting based upon finding " " characters in a string.


    Public Class Form1
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Fullname As String = TextBox1.Text.Trim
        If Fullname.Contains(" ") Then
          Dim parts As String() = Fullname.Split(" ")
          TextBox2.Text = parts(0)
          TextBox3.Text = parts(1)
          TextBox2.Text = Fullname
          TextBox3.Text = ""
        End If
      End Sub
    End Class
    Thursday, March 17, 2011 10:31 PM