-
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
-
Code:
Dim FieldLen as integer
Dim LsStr as string
FieldLen=20
LsStr = "Hello"
LsStr = LsStr & Space$(FieldLen-len(LsStr))
-
Thanks for the speedy response.
-
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.