VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Replace all instances of one string with another

by Cf890 (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Sat 13th January 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Replace all instances of one string with another

Rate Replace all instances of one string with another



' 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


Download this snippet    Add to My Saved Code

Replace all instances of one string with another Comments

No comments have been posted about Replace all instances of one string with another. Why not be the first to post a comment about Replace all instances of one string with another.

Post your comment

Subject:
Message:
0/1000 characters