Insert Strings into a String


option Explicit
'
' Paste all of this code into a BAS module
'
Enum ALIGN
    iLEFT = 0
    iCENTER = 1
    iRIGHT = 2
End Enum


'==================================================================
'
' Name...: sInsertString
' Type...: Function
' date...: 07/29/1998
' Author.: John Stendor
' Purpose: Insert a string into another string,
' using ENUM define ALIGN type
' This function is used to create reports when you want to
' format header lines.
' Additonally, uou can use it to peform small padding and
' alignment problems
'
' Example:
'
'   sHeader = Space$(80)
'   sHeader = sInsertString(Space$(80), "YOUR COMPANY NAME",
'             ALIGN.iCENTER)
'   sHeader = sInsertString(sHeader, date$, ALIGN.iLEFT)
'   sHeader = sInsertString(sHeader, ("PAGE: " & Str$(iPage)),
'             ALIGN.iRIGHT)
'
'==================================================================

'

public Function sInsertString(sStrIn as string, sStrInsert as string, _
       iAlign as Integer) as string

    Dim sTemp as string
    Dim iStrOffset as Integer


    '--------------------------------------------------------------
    ' Lets get busy and insert the string ....
    '--------------------------------------------------------------
    If len(sStrIn) then
        '----------------------------------------------------------
        ' If the input string is smaller than the insert string
        ' then pad the input string
        '----------------------------------------------------------
        If len(sStrIn) < len(sStrInsert) then
            sTemp = sStrIn & Space$(len(sStrInsert) - len(sStrIn))
        else
            sTemp = sStrIn
        End If
    else
        '----------------------------------------------------------
        ' If the input string is blank then pad with spaces the size
        ' of the insert string ....
        '----------------------------------------------------------
        sTemp = Space$(len(sStrInsert))
    End If
    '--------------------------------------------------------------
    ' Insert the sring with the alignment definition code ...
    '--------------------------------------------------------------
    Select Case (iAlign)
            '------------------------------------------------------
            ' Insert the string from the left ....
            '------------------------------------------------------
        Case ALIGN.iLEFT
            mid$(sTemp, 1) = sStrInsert
            '------------------------------------------------------
            ' Insert the string in the center ....
            '------------------------------------------------------
        Case ALIGN.iCENTER
            iStrOffset = (len(sStrIn) - len(sStrInsert))  2
            mid$(sTemp, iStrOffset) = sStrInsert
            '------------------------------------------------------
            ' Insert the string from the right ....
            '------------------------------------------------------
        Case ALIGN.iRIGHT
            iStrOffset = len(sStrIn) - len(sStrInsert)
            mid$(sTemp, iStrOffset) = sStrInsert
            '------------------------------------------------------
            ' When all else fails just assign the string and get
            ' outta here ....
            '------------------------------------------------------
        Case else
            sTemp = sStrInsert
    End Select

    sInsertString = sTemp
End Function

Download this Bas Module (2k)

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read