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

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds