|
-
Jun 1st, 2005, 05:14 PM
#1
Thread Starter
Admodistrator
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!
-
Jun 2nd, 2005, 04:24 PM
#2
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
Has someone helped you? Then you can Rate their helpful post. 
-
Jun 2nd, 2005, 06:38 PM
#3
Thread Starter
Admodistrator
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?
-
Jun 2nd, 2005, 06:44 PM
#4
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
-
Jun 2nd, 2005, 06:49 PM
#5
Thread Starter
Admodistrator
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
-
Jun 2nd, 2005, 06:52 PM
#6
Thread Starter
Admodistrator
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
-
Jun 3rd, 2005, 02:15 PM
#7
-
Jun 3rd, 2005, 03:54 PM
#8
Thread Starter
Admodistrator
Re: Multiple Replace( with just 1 statement
doesnt yours do that to? i mean it has to, i guess thats just a limitation :/
-
Jun 3rd, 2005, 03:59 PM
#9
-
Jun 3rd, 2005, 04:32 PM
#10
Thread Starter
Admodistrator
Re: Multiple Replace( with just 1 statement
yep : D we got some good posts here : )
-
Jun 3rd, 2005, 04:44 PM
#11
-
Jun 3rd, 2005, 11:22 PM
#12
Thread Starter
Admodistrator
Re: Multiple Replace( with just 1 statement
heck yes, ill work on it later
-
Jun 3rd, 2005, 11:28 PM
#13
Thread Starter
Admodistrator
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 : .
-
Jun 3rd, 2005, 11:56 PM
#14
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...
-
Jun 4th, 2005, 12:43 AM
#15
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
-
Jun 4th, 2005, 12:46 AM
#16
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 ??
-
Jun 4th, 2005, 10:03 AM
#17
Thread Starter
Admodistrator
Re: Multiple Replace( with just 1 statement
really cool! great job cv!
-
Jun 13th, 2005, 08:34 PM
#18
Thread Starter
Admodistrator
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|