Question finding spaces in a string

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:
Well I thought it would be as easy as after assigning the last name value then to check if the last name value had any spaces and then split that. Well whatever I coded didn't work but I think I'm onto something. Here's my code, what am I doing wrong?

VB.NET:
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


        firstname = fullname.Substring(0, indexspace)
        lastname = fullname.Substring(indexspace)
        middlename = lastname.Substring(indexSecondSpace)



        indexSecondSpace = lastname.IndexOf(" ")
        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
            lblName.Text = lastname & ", " & firstname & " " & middlename
        End If



    End Sub
 
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.



VB.NET:
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
 
Back
Top