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

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT 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 upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds