CodeItBetter Programming Another VB Programming Blog

How to Replace Multiple Substrings in a String at Once

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'String Manipulation - How to Replace Multiple Substrings in a String at Once
Option Explicit
 
Public Function ReplaceMultiple(ByVal Expression As String, ByVal sReplace As String, _
    ParamArray sFind()) As String
 
'Replaces multiple substrings in a string with the character or string specified by sReplace
'Overlap Between Characters in sReplace and sFind Will cause this function to behave
'incorrectly unless you are careful about the order of strings in sFind

    Dim lLBound As Long, lUBound As Long, lCount As Long
    Dim sResult As String
 
    lLBound = LBound(sFind)
    lUBound = UBound(sFind)
 
    sResult = Expression
 
    For lCount = lLBound To lUBound
        sResult = Replace(sResult, CStr(sFind(lCount)), sReplace)
    Next
 
    ReplaceMultiple = sResult
End Function
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.