Results 1 to 18 of 18

Thread: Multiple Replace( with just 1 statement

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Multiple Replace( with just 1 statement

    Just made this cool function, because i was sick of using like 40 replace( things
    VB Code:
    1. Option Explicit
    2. Dim StrRep() As String
    3. Dim i As Integer
    4. Dim EndStr As String
    5.  
    6. Private Sub Form_Load()
    7. Call ReplaceStr("Hello there cat, its 4:32 PM", ":!!! cat!!!t!!!")''''PUT THE THINGS YOU WANT TO SPLIT BY INBETWEEN THE !!!!, OR ADD MORE WITH !!!
    8. End Sub
    9.  
    10. Public Function ReplaceStr(BaseStr As String, StrToReplace As String)
    11.     StrRep() = Split(StrToReplace, "!!!")
    12.         For i = 0 To UBound(StrRep)
    13.                 If i = 0 Then
    14.            EndStr = Replace(BaseStr, StrRep(i), vbNullString)
    15.                 Else
    16.            EndStr = Replace(EndStr, StrRep(i), vbNullString)
    17.                 End If
    18.         Next i
    19.         MsgBox EndStr
    20. End Function

    enjoy!

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Multiple Replace( with just 1 statement

    Slightly different but the same (different way of passing the strings to replace) :

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim S As String
    5.    
    6.     S = "Hello there cat, its 4:32 PM"
    7.    
    8.     MsgBox ReplaceStr(S, ":", "cat", "t")
    9. End Sub
    10.  
    11. Public Function ReplaceStr(BaseStr As String, ParamArray StrToReplace() As Variant)
    12.     Dim EndStr As String
    13.     Dim i As Integer
    14.    
    15.     EndStr = BaseStr
    16.    
    17.     For i = 0 To UBound(StrToReplace)
    18.         EndStr = Replace(EndStr, StrToReplace(i), vbNullString)
    19.     Next i
    20.    
    21.     ReplaceStr = EndStr
    22. End Function


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Multiple Replace( with just 1 statement

    thats a cool one, but please answer something for me

    ParamArray StrToReplace() As Variant

    by adding the () does it allow you to have many ,'s?
    or what does it so you can use ReplaceStr(S, ":", "cat", "t")
    and its valid?

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Multiple Replace( with just 1 statement

    It simply puts all the parameters in an Array... that way you can call the function with as many params as you want, it the equivalent of "..." in C (Not sure if C++ also)

    By the way, you should also have a param for what you want to replace with, THEN it will be a lot more helpfull...

    [edit]
    Something like this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim S As String
    5.    
    6.     S = "Hello there cat, its 4:32 PM"
    7.    
    8.     MsgBox ReplaceStr(S, " ", ":", "cat", "t")
    9. End Sub
    10.  
    11. Public Function ReplaceStr(BaseStr As String, ReplWith As String, ParamArray StrToReplace() As Variant)
    12.     Dim EndStr As String
    13.     Dim i As Integer
    14.    
    15.     EndStr = BaseStr
    16.    
    17.     For i = 0 To UBound(StrToReplace)
    18.         EndStr = Replace(EndStr, StrToReplace(i), ReplWith)
    19.     Next i
    20.    
    21.     ReplaceStr = EndStr
    22. End Function

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Multiple Replace( with just 1 statement

    good point, yeah thats a cool feature, ill add it

    i thought i was the first person to think of this

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Multiple Replace( with just 1 statement

    VB Code:
    1. Option Explicit
    2. Dim StrRep() As String
    3. Dim i As Integer
    4. Dim EndStr As String
    5.  Private Sub Form_Load()
    6. Call ReplaceStr("Hello there cat, its 4:32 PM", ":!!! cat!!!t!!!", "g") ''''PUT THE THINGS YOU WANT TO SPLIT BY INBETWEEN THE !!!!, OR ADD MORE WITH !!!
    7. End Sub
    8.  Public Function ReplaceStr(BaseStr As String, StrToReplace As String, ReplaceWith As String)
    9.     StrRep() = Split(StrToReplace, "!!!")
    10.         For i = 0 To UBound(StrRep)
    11.                 If i = 0 Then
    12.            EndStr = Replace(BaseStr, StrRep(i), ReplaceWith)
    13.                 Else
    14.            EndStr = Replace(EndStr, StrRep(i), ReplaceWith)
    15.                 End If
    16.         Next i
    17.         MsgBox EndStr
    18. End Function
    i was gonna change it to his way, but i figure its better to have more than one way to do something

  7. #7
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Multiple Replace( with just 1 statement

    Another thing that would cause a problem is the order in which you replace (with all the versions so far)... If you were to replace the "t" before "cat", then "cat" wouldn't be in the original string anymore and you would have a "ca" left in the string...


    Has someone helped you? Then you can Rate their helpful post.

  8. #8

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Multiple Replace( with just 1 statement

    doesnt yours do that to? i mean it has to, i guess thats just a limitation :/

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Multiple Replace( with just 1 statement

    All of them do, it's because of the way replace works... If you sort the array by length, and then you replace in that order (longest to shorter) it will work fine (I think)


    Has someone helped you? Then you can Rate their helpful post.

  10. #10

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Multiple Replace( with just 1 statement

    yep : D we got some good posts here : )

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Multiple Replace( with just 1 statement

    You want to gather all the info together and make a final (fool proof) version of the function?


    Has someone helped you? Then you can Rate their helpful post.

  12. #12

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Multiple Replace( with just 1 statement

    heck yes, ill work on it later

  13. #13

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Multiple Replace( with just 1 statement

    okay, quoting you here:
    If you sort the array by length, and then you replace in that order (longest to shorter) it will work fine (I think)
    how would i do this? im trying to brainstorm but its not connecting : .

  14. #14
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Multiple Replace( with just 1 statement

    You mean sorting ?

    A simple buble sort is good enough (since you never have lots of params)
    But instead of comparing the data, compare the length....

    Let me try, If i'm faster than you, i'll post it...

  15. #15
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Multiple Replace( with just 1 statement

    Here it is:

    I changed it so you can pass any value type (it will be converted to string), and also, you can pass string arrays (but those will not sort)
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim S As String, Test(2) As String
    5.    
    6.     Test(0) = ":"
    7.     Test(1) = "t"
    8.     Test(2) = "cat"
    9.    
    10.     S = "Hello there cat, its 4:32 PM"
    11.    
    12.     Debug.Print ReplaceStr(S, ".", Test, "ll", 4, 32)  ' Test array will not sort...
    13.    
    14.     Debug.Print ReplaceStr(S, " ", ":", "t", "cat", 4)
    15.     Debug.Print ReplaceStr(S, " ", ":", "cat", "t", 4)
    16.    
    17.     Unload Me
    18. End Sub
    19.  
    20. Public Function ReplaceStr(ByVal BaseStr As String, ReplWith As String, ParamArray StrToReplace() As Variant)
    21.     Dim T As Variant, I As Long, K As Long
    22.    
    23.     ' Convert whatever you can to string
    24.     On Error Resume Next
    25.     For K = LBound(StrToReplace) To UBound(StrToReplace)
    26.         If Not ((VarType(StrToReplace(K)) And vbArray) = vbArray) And Not VarType(StrToReplace(K)) = vbString Then
    27.             ' when it cannot convert, it will ignore the error, and leave the same value
    28.             StrToReplace(K) = CStr(StrToReplace(K))
    29.         End If
    30.     Next K
    31.     On Error GoTo 0
    32.    
    33.     ' Sort the array
    34.     For K = LBound(StrToReplace) To UBound(StrToReplace)
    35.         For I = K + 1 To UBound(StrToReplace)
    36.             If VarType(StrToReplace(K)) = vbString And VarType(StrToReplace(I)) = vbString Then
    37.                 If Len(StrToReplace(K)) < Len(StrToReplace(I)) Then
    38.                    
    39.                     ' SWAP VALUES
    40.                     T = StrToReplace(K)
    41.                     StrToReplace(K) = StrToReplace(I)
    42.                     StrToReplace(I) = T
    43.                 End If
    44.             End If
    45.         Next I
    46.     Next K
    47.    
    48.     ' Replace
    49.     For I = 0 To UBound(StrToReplace)
    50.         If VarType(StrToReplace(I)) = vbString Then
    51.             BaseStr = Replace(BaseStr, StrToReplace(I), ReplWith)
    52.         ElseIf VarType(StrToReplace(I)) = vbArray + vbString Then
    53.            
    54.             ' If input is a String Array, loop through and replace those too
    55.             For K = LBound(StrToReplace(I)) To UBound(StrToReplace(I))
    56.                 BaseStr = Replace(BaseStr, StrToReplace(I)(K), ReplWith)
    57.             Next K
    58.         End If
    59.     Next I
    60.    
    61.     ReplaceStr = BaseStr
    62. End Function

  16. #16
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Multiple Replace( with just 1 statement

    I just realized, now you can call the function like this:
    VB Code:
    1. Debug.Print ReplaceStr(S, " ", Split(":!!! cat!!!t!!!", "!!!"))

    Neet, eh ??

  17. #17

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Multiple Replace( with just 1 statement

    really cool! great job cv!

  18. #18

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Multiple Replace( with just 1 statement

    can someone tell me how each compares to the regular replace?

    Use replace 500 -1000 times, and do the multireplace with the same value and see how long it takes..thanks

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