PDA

Click to See Complete Forum and Search --> : Multiple Replace( with just 1 statement


|2eM!x
Jun 1st, 2005, 05:14 PM
Just made this cool function, because i was sick of using like 40 replace( things
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:

manavo11
Jun 2nd, 2005, 04:24 PM
Slightly different but the same (different way of passing the strings to replace) :

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

|2eM!x
Jun 2nd, 2005, 06:38 PM
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?

CVMichael
Jun 2nd, 2005, 06:44 PM
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:

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

|2eM!x
Jun 2nd, 2005, 06:49 PM
good point, yeah thats a cool feature, ill add it :)

i thought i was the first person to think of this ;)

|2eM!x
Jun 2nd, 2005, 06:52 PM
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

manavo11
Jun 3rd, 2005, 02:15 PM
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...

|2eM!x
Jun 3rd, 2005, 03:54 PM
doesnt yours do that to? i mean it has to, i guess thats just a limitation :/

manavo11
Jun 3rd, 2005, 03:59 PM
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) :)

|2eM!x
Jun 3rd, 2005, 04:32 PM
yep : D we got some good posts here : )

manavo11
Jun 3rd, 2005, 04:44 PM
You want to gather all the info together and make a final (fool proof) version of the function? ;)

|2eM!x
Jun 3rd, 2005, 11:22 PM
heck yes, ill work on it later :D

|2eM!x
Jun 3rd, 2005, 11:28 PM
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 : .

CVMichael
Jun 3rd, 2005, 11:56 PM
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... :)

CVMichael
Jun 4th, 2005, 12:43 AM
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)

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

CVMichael
Jun 4th, 2005, 12:46 AM
I just realized, now you can call the function like this:

Debug.Print ReplaceStr(S, " ", Split(":!!! cat!!!t!!!", "!!!"))


Neet, eh ??

|2eM!x
Jun 4th, 2005, 10:03 AM
really cool! great job cv!

|2eM!x
Jun 13th, 2005, 08:34 PM
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