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)



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds