Formatting Strings


This function allows the user to left, right, or center text within a specified length.

EG.

format "Hello" right justified within a length of 10 characters.

" Hello"

option Explicit

Const LJ = 0
Const RJ = 1
Const CJ = 2

Function FormatLen(StringtoFormat as Variant, _
                   LengthtoFormat as Variant, _
                   optional Justify as Variant) as string

    Dim Temp as string
    
    If IsNumeric(LengthtoFormat) = true And IsNumeric(Justify) = _
                                   true then
        If len(StringtoFormat) >= LengthtoFormat then
            FormatLen = Left$(StringtoFormat, LengthtoFormat)
        else
            If IsMissing(Justify) Or Justify = LJ then
                Temp = StringtoFormat & Space(LengthtoFormat - _
                       len(StringtoFormat))
                FormatLen = Temp
            ElseIf Justify = RJ then
                Temp = Space(LengthtoFormat - len(StringtoFormat)) & _
                       StringtoFormat
                FormatLen = Temp
            ElseIf Justify = CJ then
                 Temp = Space((LengthtoFormat \ 2) - (len(StringtoFormat) _
                        \ 2))
                 Temp = Temp & StringtoFormat
                 Temp = Temp & Space(LengthtoFormat - len(Temp))
                 FormatLen = Temp
            else
                MsgBox "Could not format string!", vbOKOnly
            End If
        End If
    else
        MsgBox "Could not format string!", vbOKOnly
    End If

End Function

Download Format.bas (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

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Historically, IT investments were made simply in the interest of increasing competitiveness and with little insight into the hard, fact-based financial case. As organizations worldwide have been forced to tighten belts and slash budgets, management increasingly require financial evidence for sound IT purchases that fully align with business goals and strategies. This webinar defines and describes total cost of ownership - an important metric for assessing and tracking the risks, costs, and benefits of …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds