Multiple Replace( with just 1 statement
Just made this cool function, because i was sick of using like 40 replace( things
VB Code:
Option Explicit
Dim StrRep() As String
Dim i As Integer
Dim EndStr As String
Private Sub Form_Load()
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 !!!
End Sub
Public Function ReplaceStr(BaseStr As String, StrToReplace As String)
StrRep() = Split(StrToReplace, "!!!")
For i = 0 To UBound(StrRep)
If i = 0 Then
EndStr = Replace(BaseStr, StrRep(i), vbNullString)
Else
EndStr = Replace(EndStr, StrRep(i), vbNullString)
End If
Next i
MsgBox EndStr
End Function
enjoy! :duck:
Re: Multiple Replace( with just 1 statement
Slightly different but the same (different way of passing the strings to replace) :
VB Code:
Option Explicit
Private Sub Form_Load()
Dim S As String
S = "Hello there cat, its 4:32 PM"
MsgBox ReplaceStr(S, ":", "cat", "t")
End Sub
Public Function ReplaceStr(BaseStr As String, ParamArray StrToReplace() As Variant)
Dim EndStr As String
Dim i As Integer
EndStr = BaseStr
For i = 0 To UBound(StrToReplace)
EndStr = Replace(EndStr, StrToReplace(i), vbNullString)
Next i
ReplaceStr = EndStr
End Function
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?
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:
Option Explicit
Private Sub Form_Load()
Dim S As String
S = "Hello there cat, its 4:32 PM"
MsgBox ReplaceStr(S, " ", ":", "cat", "t")
End Sub
Public Function ReplaceStr(BaseStr As String, ReplWith As String, ParamArray StrToReplace() As Variant)
Dim EndStr As String
Dim i As Integer
EndStr = BaseStr
For i = 0 To UBound(StrToReplace)
EndStr = Replace(EndStr, StrToReplace(i), ReplWith)
Next i
ReplaceStr = EndStr
End Function
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 ;)
Re: Multiple Replace( with just 1 statement
VB Code:
Option Explicit
Dim StrRep() As String
Dim i As Integer
Dim EndStr As String
Private Sub Form_Load()
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 !!!
End Sub
Public Function ReplaceStr(BaseStr As String, StrToReplace As String, ReplaceWith As String)
StrRep() = Split(StrToReplace, "!!!")
For i = 0 To UBound(StrRep)
If i = 0 Then
EndStr = Replace(BaseStr, StrRep(i), ReplaceWith)
Else
EndStr = Replace(EndStr, StrRep(i), ReplaceWith)
End If
Next i
MsgBox EndStr
End Function
i was gonna change it to his way, but i figure its better to have more than one way to do something :p
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...
Re: Multiple Replace( with just 1 statement
doesnt yours do that to? i mean it has to, i guess thats just a limitation :/
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) :)
Re: Multiple Replace( with just 1 statement
yep : D we got some good posts here : )
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? ;)
Re: Multiple Replace( with just 1 statement
heck yes, ill work on it later :D
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 : .
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... :)
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:
Option Explicit
Private Sub Form_Load()
Dim S As String, Test(2) As String
Test(0) = ":"
Test(1) = "t"
Test(2) = "cat"
S = "Hello there cat, its 4:32 PM"
Debug.Print ReplaceStr(S, ".", Test, "ll", 4, 32) ' Test array will not sort...
Debug.Print ReplaceStr(S, " ", ":", "t", "cat", 4)
Debug.Print ReplaceStr(S, " ", ":", "cat", "t", 4)
Unload Me
End Sub
Public Function ReplaceStr(ByVal BaseStr As String, ReplWith As String, ParamArray StrToReplace() As Variant)
Dim T As Variant, I As Long, K As Long
' Convert whatever you can to string
On Error Resume Next
For K = LBound(StrToReplace) To UBound(StrToReplace)
If Not ((VarType(StrToReplace(K)) And vbArray) = vbArray) And Not VarType(StrToReplace(K)) = vbString Then
' when it cannot convert, it will ignore the error, and leave the same value
StrToReplace(K) = CStr(StrToReplace(K))
End If
Next K
On Error GoTo 0
' Sort the array
For K = LBound(StrToReplace) To UBound(StrToReplace)
For I = K + 1 To UBound(StrToReplace)
If VarType(StrToReplace(K)) = vbString And VarType(StrToReplace(I)) = vbString Then
If Len(StrToReplace(K)) < Len(StrToReplace(I)) Then
' SWAP VALUES
T = StrToReplace(K)
StrToReplace(K) = StrToReplace(I)
StrToReplace(I) = T
End If
End If
Next I
Next K
' Replace
For I = 0 To UBound(StrToReplace)
If VarType(StrToReplace(I)) = vbString Then
BaseStr = Replace(BaseStr, StrToReplace(I), ReplWith)
ElseIf VarType(StrToReplace(I)) = vbArray + vbString Then
' If input is a String Array, loop through and replace those too
For K = LBound(StrToReplace(I)) To UBound(StrToReplace(I))
BaseStr = Replace(BaseStr, StrToReplace(I)(K), ReplWith)
Next K
End If
Next I
ReplaceStr = BaseStr
End Function
Re: Multiple Replace( with just 1 statement
I just realized, now you can call the function like this:
VB Code:
Debug.Print ReplaceStr(S, " ", Split(":!!! cat!!!t!!!", "!!!"))
Neet, eh ??
Re: Multiple Replace( with just 1 statement
really cool! great job cv!
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