Replace all instances of one string with another
Replace all instances of one string with another
Rate Replace all instances of one string with another
(1(1 Vote))
' Can also remove strings by setting newStr as ""
' Robust and fully working for every type of string replacement
' including 'old in new' e.g. StrReplace("aabca", "a", "Hat")
Public Function strReplace(ByVal str As String, oldStr As String, newStr As String) As String
Dim tmpStr As String
Dim start As Long
tmpStr = str
start = 1
i = InStr(tmpStr, oldStr)
Do While i
tmpStr = Mid(tmpStr, 1, i - 1) & newStr & _
Mid(tmpStr, i + Len(oldStr), Len(tmpStr))
start = i + Len(newStr)
i = InStr(start, tmpStr, oldStr)
Loop
strReplace = tmpStr
End Function
Replace all instances of one string with another Comments
No comments yet — be the first to post one!
Post a Comment