Ok I'm trying to program in vb.net and 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.
(stay with me this kinda gets confusing)
I feel like I'm really close and then it blows up on me. So the way I see it is this: firstname equals the full name up until the first space. Then lastname equals the full name after the first space. then I assign the secondspace to equal the first space in the last name. Then I assign the middle name to equal whats in the last name up until the first space of the last name (second space for the full text entry) and then to finish it off I reassign lastname to be whatevers after the second space. As you can see I have the label show me the value of the second space and its always zero when it should be reading 1. Why isnt it reading 1 when it should be? After thinking about this so hard I'm surprised I havn't gone cross eyed. Save me from insanity, thanks.
(stay with me this kinda gets confusing)
I feel like I'm really close and then it blows up on me. So the way I see it is this: firstname equals the full name up until the first space. Then lastname equals the full name after the first space. then I assign the secondspace to equal the first space in the last name. Then I assign the middle name to equal whats in the last name up until the first space of the last name (second space for the full text entry) and then to finish it off I reassign lastname to be whatevers after the second space. As you can see I have the label show me the value of the second space and its always zero when it should be reading 1. Why isnt it reading 1 when it should be? After thinking about this so hard I'm surprised I havn't gone cross eyed. Save me from insanity, thanks.
Code:
Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
Dim fullname As String
Dim indexspace As Integer
Dim firstname As String
Dim lastname As String
Dim indexSecondSpace As Integer
Dim middlename As String
fullname = txtName.Text.Trim
fullname = fullname.Replace(".", "")
txtName.Text = fullname
indexspace = fullname.IndexOf(" ")
firstname = fullname.Substring(0, indexspace)
lastname = fullname.Substring(indexspace)
indexSecondSpace = lastname.IndexOf(" ")
middlename = lastname.Substring(0, indexSecondSpace)
lastname = fullname.Substring(indexSecondSpace)
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
lblName.Text = indexSecondSpace.ToString
End If
End Sub