Results 1 to 4 of 4

Thread: Padding Out a String

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 1999
    Location
    Belfast
    Posts
    254
    Hi,
    can some one let me know how to pad out a string.

    i.e. I declare a string

    dim ls_str as string

    I then find the size of a foeld in a database. I set the value of the string. If the string size is smaller than the field size I want to pad the string out to the size of the field with spaces.



    Hope this is clear.

    Thanks in advance.

    Lenin


  2. #2
    Addicted Member
    Join Date
    Apr 2000
    Location
    Sheffield, England.
    Posts
    136
    Code:
    Dim FieldLen as integer
    Dim LsStr as string
    
    FieldLen=20
    LsStr = "Hello"
    
    LsStr = LsStr & Space$(FieldLen-len(LsStr))
    Visual Basic 6 Enterprise Edition + SP4

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 1999
    Location
    Belfast
    Posts
    254
    Thanks for the speedy response.

  4. #4
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Why not use

    Code:
    Dim LsStr As String * 20
    
    LsStr = "Hello"
    MsgBox "#" & LsStr & "#"
    You'll see that even though you only put "Hello" into the string you will ALWAYS get 20 characters out (that's what the * 20 means) - so it is automatically padded with spaces.
    The one thing to look out for is when you are comparing variables;

    Code:
    If LsStr = "Hello" Then
       Msgbox "TRUE"
    Else
       Msgbox "FALSE"
    End If
    
    will return FALSE because "Hello" does not equal "Hello                "
    In these instances you should use TRIM; eg;

    Code:
    If Trim(LsStr) = "Hello" Then
       Msgbox "TRUE"
    Else
       Msgbox "FALSE"
    End If
    TRIM removes any spaces from around the contents of LsStr and compares that with "Hello" - which will return TRUE.

    Hope this helps.

    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width