audio_uphoria
Active member
- Joined
- Jan 28, 2009
- Messages
- 31
- Programming Experience
- Beginner
Hello, I need some help trying to find a space in a string that has more than one space. I want to be able to find both spaces using the indexof method. Here's my code so far. Basically the program will turn the string "Will Smith" to "Smith, Will" But I noticed when I type in a name with a middle initial or middle name "Will J Smith" I would get "J Smith, Will" And I want to be able to find that second space when its there and apply a code that would produce "Smith, Will J" When there is a second space. How would I go about doing so? Thanks!
VB.NET:
Dim fullname As String
Dim indexspace As Integer
Dim firstname As String
Dim lastname As String
fullname = txtName.Text.Trim
txtName.Text = fullname
indexspace = fullname.IndexOf(" ")
If indexspace < 0 Then
MessageBox.Show("Please enter your first name followed by a space and your last name into the text box", "Error")
spaceCheckOk = False
txtName.Focus()
Else
spaceCheckOk = True
End If
If spaceCheckOk = True Then
firstname = fullname.Substring(0, indexspace)
lastname = fullname.Substring(indexspace)
lblName.Text = indexspace.ToString
End If
End Sub
Last edited: