Results 1 to 6 of 6

Thread: Formatting String

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Formatting String

    Hi Guys,

    Could Anybody help me to format the string

    aa-aa >> aa - aa
    abc -ghi >> abc - ghi

    In case it's aa - aa, then we don't do nothing

    Thanks for your help.

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Re: Formatting String

    yes i know the repleace function , however sometimes the string appear like hello-world, hello -world,hello- word....

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Formatting String

    So you can do something like this but I'm afraid you'd have to loop through entire string and parse it:
    Code:
    Private Sub Command2_Click()
        'thry this
        Debug.Print Replace(Replace("hello-world, hello -world,hello- word", "-", " - "), Space(2), Space(1))
    
        'Or this:
        Debug.Print Replace(Replace("hello-world, hello -world,hello- word", Space(1), ""), "-", " - ")
    End Sub

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Formatting String

    Is it just 2 parts with one dash in the middle? ie:

    XX- XX
    YY -YY
    YY - YY


    And not like:

    XX- XX - XX - XX etc...

    This is a quick/lazy way to do it. If you're dealing with a bunch of data then there are faster ways:

    Code:
    Option Explicit
    
    'aa-aa >> aa - aa
    'abc -ghi >> abc - ghi
    Private Sub FixString(TheString As String)
        Dim strB() As String
        
        strB() = Split(TheString, "-")
        TheString = Trim$(strB(0)) & " - " & Trim$(strB(1))
        Erase strB
    End Sub
    
    Private Sub Form_Load()
        Dim s As String
        
        s = "aa   -  aa"
        FixString s
        MsgBox s
    End Sub

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

    Re: Formatting String

    Nuts. Here was my nearly identical effort
    Code:
    Private Function Reformat(MyString)
       Dim MyArray() As String
       MyArray = Split(MyString, "-")
       Reformat = Trim(MyArray(0) & " - " & Trim(MyArray(1)))
    End Function
    
    Private Sub Form_Load()
    MsgBox Reformat("aaa- aaa")
    End Sub
    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