by Prashant Sharma (4 Submissions)
Category: String Manipulation
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Thu 24th October 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
this code implements the REPLACE function in VB5 or lower, which comes in VB6. It is the exact implementation and works similar to the details
'Author: Prashant Sharma
'Web-site: http://www.prashantsharma/com
'eMail: [email protected]
'Copy and paste this function in a module
Public Function Replace(ByVal sExp As String, ByVal sFind As String, ByVal sReplace As String, Optional ByVal lStart As Long = 1, Optional ByVal lCount As Long = -1, Optional ByVal Compare As VbCompareMethod = vbBinaryCompare) As String
Dim i As Long, j As Long, idx As Long, fl As Long, rl As Long
idx = 0
fl = Len(sFind)
rl = Len(sReplace)
i = lStart
j = InStr(i, sExp, sFind, Compare)
'Loop only if needed
Do While j > 0
If idx = lCount Then Exit Do
sExp = Left$(sExp, j - 1) & sReplace & Mid$(sExp, j + fl)
idx = idx + 1
i = j + rl
j = InStr(i, sExp, sFind, Compare)
Loop
Replace$ = sExp
End Function
'The sample usage is given here, copy and paste it in a form...
Private Sub Form_Load()
Dim sSrc As String, sDst As String, sFind As String, sRepl As String
sSrc = "This is a test"
sFind = "a"
sRepl = "NOT"
sDst = Replace(sSrc, sFind, sRepl)
MsgBox sDst
End Sub
No comments have been posted about this code implements the REPLACE function in VB5 or lower, which comes in VB6. It is the exact impl. Why not be the first to post a comment about this code implements the REPLACE function in VB5 or lower, which comes in VB6. It is the exact impl.