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: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

  • Confused by all the agile advice? Relax! With the Agile for Dummies eBook by your side you'll learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill customer needs much faster.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds