Results 1 to 13 of 13

Thread: [RESOLVED] String seperation

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    Resolved [RESOLVED] String seperation

    Hi there..

    How do I seperate one string, into two seperate strings?
    If i want to divide a Name, say Jack Johnson, into first name and last name, and write the first name in a seperate column, and the last name in a seperate column as well?

    Thx for your time..

    Bjede

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: String seperation

    Welcome to the forums.
    Code:
    Dim arrName() As String
    Dim strName As String
    strName = "Jack Johnson"
    arrName = Split(strName, " ")
    Text1.Text = arrName(0)
    Text2.Text = arrName(1)

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    Re: String seperation

    Hi Hack..

    Thx for your quick response.. What if the name consists of a for, middle and a last name, and for- and middle name needs to "one" string..

    /Bjede

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: String seperation

    They won't be one string, but you can display then as if they were.

    If there are three names, then you will have arrName(0), arrName(1) and arrName(2)

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    Re: String seperation

    Super.. it works.. thx Hack..

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: String seperation

    Do you have control over how the name is being saved? If so, save the name in a format you can easily parse. Some names won't have middle initials, some may have 2 or more, some last names can be hyphenated, some first names are actually two names (like Jo Ann), some names are only initials (actor: D.B. Sweeney). There are many variations.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: String seperation

    If you could have a name like "Eddie Van Halen" or "Frans van der Hoff" then you should modify Hack's code like this

    Code:
    Dim arrName() As String
    Dim strName As String
    Dim lngIndex As Long
    strName = "Eddie Van Halen" 'Frans van der Hoff
    arrName = Split(strName, " ")
    Text1.Text = arrName(0)
    Text2.Text = arrName(1)
    For lngIndex = 2 To UBound(arrName)
        Text2.Text = Text2.Text & " " & arrName(lngIndex)
    Next

  8. #8
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: String seperation

    you could do this (slightly more complitcated) code that will always separate at the last space:
    Code:
    n$ = "Jack Johansen Johnson"
    a = instrrev(n$, " ")
    firstname = left(n$, a - 1)
    lastname = right(n$, len(n$) - len(firstname) - 1
    text1.text = firstname
    text2.text = lastname
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: String seperation

    If you don't know how many names you will be dealing with (2 or 3) then try something like this
    Code:
    Private Sub Command1_Click()
    Dim arrName() As String
    Dim strName As String
    strName = Text4.Text
    arrName = Split(strName, " ")
    Select Case UBound(arrName)
        Case 2 'we have a middle name
           Text1.Text = arrName(0) & " " & arrName(1)
           Text2.Text = arrName(2)
        Case 1 'we have no middle name
           Text1.Text = arrName(0)
           Text2.Text = arrName(1)
    End Select
    End Sub

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    Re: String seperation

    No, not really.. I do have some problems, when there are no middlename.. I need somehow to be able to know whether I have a middlename or not.. is it possible to do some sort of test, to see how many names there is in my array?

  11. #11

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    Re: String seperation

    just worked out.. thx guys..

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: String seperation

    Quote Originally Posted by Hack
    If you don't know how many names you will be dealing with (2 or 3) then try something like this
    Code:
    Private Sub Command1_Click()
    Dim arrName() As String
    Dim strName As String
    strName = Text4.Text
    arrName = Split(strName, " ")
    Select Case UBound(arrName)
        Case 2 'we have a middle name
           Text1.Text = arrName(0) & " " & arrName(1)
           Text2.Text = arrName(2)
        Case 1 'we have no middle name
           Text1.Text = arrName(0)
           Text2.Text = arrName(1)
    End Select
    End Sub
    My code in post #7 does the same.

  13. #13
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: String seperation

    Quote Originally Posted by Bjede
    No, not really.. I do have some problems, when there are no middlename.. I need somehow to be able to know whether I have a middlename or not.. is it possible to do some sort of test, to see how many names there is in my array?
    you can get the upper bound of an array really easy
    Code:
    dim MyArray(1 to 5)
    msgbox ubound(myarray)
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width