About   -   Contact   -   Purchase   -   Search   -   What's New

Replace Function for VB4/5
This function is intended to offer the same functionality as the Replace command found in VB6, which is dearly missed when returning to previous VB releases. In addition this function allows the replacement of single or multiple characters with alternate single or multiple characters. The function then passes the edited string to the calling procedure. The function searches for the characters in strOriginal to be removed, i.e. strRemoveTxt, and removes them by concatenating the fragments of strOriginal which lie either side of strRemoveTxt, around strReplaceTxt. This process is repeated until no further occurrences of strRemove exist in strOriginal. Add the following code to a FileHandling.bas standard module to compile a File Utility function set:
Public Function Replace(strOriginal As String, _
  strRemoveTxt As String, strReplaceTxt As String) _
  As String

'<DESC>		Replaces selected character(s) in string with 
'		substitute character(s)</DESC>
'<RETURN>		Replace(String):
'			Edited string</RETURN>
'<ACCESS>		Public</ACCESS>
'<ARGS>		strOriginal(String): 
'			Unedited string
'		strRemove(String):
'			Character(s) to be removed	
'		strReplace(String):
'			Character(s) to replace removed 
'			character(s)</ARGS>	
'<USAGE>		strExample = Replace("Scissors", "ss", "z")
'		</USAGE>

    Dim intPos As Integer
    Dim strTxt As String

    If strRemoveTxt = "" Then Exit Function

    strTxt = strOriginal
    intPos = InStr(strTxt, strRemoveTxt)
    Do While intPos <><> 0
        strTxt = Left$(strTxt, intPos - 1) & strReplaceTxt & _
          Mid$(strTxt, intPos + Len(strRemoveTxt))
        intPos = InStr(strTxt, strRemoveTxt)
    Replace = strTxt

End Function

Top of Page

Legal Notice

Ken Howe 2011