Source code for Issue Number 9

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste this code into any module (this is the desired way) or form.

'----------------------------------------------------------------------
'
'   Module Name:    basStrings
'   Written By:     C&D Programming Corp.
'   Create Date:    10/30/97
'   Copyright:      Copyright 1997 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'
'   Purpose:        Do a search and replace on a string
'
'
'----------------------------------------------------------------------

Option Explicit

Public Function ReplaceChars(sItemData As String, sSrc As String, sDest As String) As String
    Dim sNewStr As String
    Dim sTemp As String
    Dim iLastPos As Integer
    Dim iPos As Integer
    Dim iLenSrc As Integer

    iLastPos = 1
    iPos = iLastPos
    iLenSrc = Len(sSrc)
    sNewStr = ""
    sTemp = ""
    While iPos <> 0
        ' Search for occurences of the search string
        iPos = InStr(iLastPos, sItemData, sSrc)
        If iLastPos <> iPos Then
            If iPos = 0 Then
                sTemp = Mid$(sItemData, iLastPos)
            Else
                sTemp = Mid$(sItemData, iLastPos, iPos - iLastPos)
            End If
        Else
            sTemp = ""
        End If
        ' Position our pointer after the last occurence of
        ' the search string
        iLastPos = iPos + iLenSrc
        ' Build the new string
        If iPos = 0 Then
            sNewStr = sNewStr & sTemp
        Else
            sNewStr = sNewStr & sTemp & sDest
        End If
    Wend
    ReplaceChars = sNewStr
End Function