Results 1 to 3 of 3

Thread: Hate strings

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2010
    Posts
    23

    Hate strings

    What´s wrong with this? I have "Firstname Lastname"

    I want "Lastname" and "Firstname"


    Function Manipulate_string(ByVal strName As String)


    Dim str1() As String
    Dim str2 As String
    Dim str3() As String
    Dim str4 As String
    strName = Left(strName, Len(strName) - 1)

    str1 = Split(strName, " ")
    str2 = str1(1)

    str3 = Split(str2, " ")
    str4 = str3

    'En msgbox för att visa hur slutresultatet blir

    MsgBox (str4 + str2)
    Manipulate_string = str4

    End Function

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Hate strings

    what is actual content of strname (example) that is passed to function
    str1 = Split(strName, " ")
    str2 = str1(1)

    str3 = Split(str2, " ")
    str4 = str3
    as str2 is an element of str1, which an array split on spaces, str2 can not contain a space to be split on, so str3 is an array of 1 element, the the same content as str1(1), str4 is another array of 1 element
    the msgbox should error (type mismatch) as it can not accept an array
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Hate strings

    Is this what you are trying?

    Code:
    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim strSampleName As String
        
        strSampleName = "Firstname Lastname"
        'strSampleName = "Firstname MiddleName Lastname"
        
        MsgBox Manipulate_string(strSampleName)
    End Sub
    
    Function Manipulate_string(ByVal strName As String)
        Dim strArray() As String, i As Long
        
        strArray = Split(strName, " ")
        
        For i = UBound(strArray) To LBound(strArray) Step -1
            Manipulate_string = Manipulate_string & " " & strArray(i)
        Next
        
        Manipulate_string = Trim(Manipulate_string)
    End Function
    Edit: lolzzz Pete... you beat me to it again...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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