Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pad a string? 4

Status
Not open for further replies.

EwS

Programmer
Dec 30, 2002
398
US
I couldn't find any info in this forum, the search is very slow at this time.

How do I pad a string? I couldn't find any function that would do this. For instance, if I'm expecting a string of 10 characters, I want to convert (add spaces to) anything that's less than that to be exactly 10 characters.
Example: "ABC" -> "ABC ".

Thank you!

 
Here's just one of many methods:
[tt]
Public Function PadString(strSource As String, lPadLen As Long, Optional PadChar As String = " ") As String
PadString = String(lPadLen, Left(PadChar, 1))
Mid(PadString, 1, Len(strSource)) = strSource
End Function
 
Look up the Space command. It will generate a string of spaces of the length you specify.

For example:

MyString = "ABC"

If Len MyString < 10 Then
' Have to check to make sure that the len of the string is less than 10, otherwise you will get an error.
MyPaddedString = MyString & Space(10 - Len(MyString))
End If


Robert
 
Yep, as I said there are a number of ways of doing it...
 
...and on review my solution can be simplified (and thus speeded up slightly) to:
[tt]
Public Function PadString(strSource As String, lPadLen As Long, Optional PadChar As String = &quot; &quot;) As String
PadString = String(lPadLen, PadChar)
Mid(PadString, 1, lPadLen) = strSource
End Function
[/tt]
which, as you can probably see, is more-or-less the same as the function you found on the elsewhere on the web
 
The fastest method, as far as I know, is to use the LSet function.
Code:
Public Function PadString(strSource As String, lPadLen As Long, Optional PadChar As String = &quot; &quot;) As String
    PadString = String(lPadLen , PadChar )
    LSet PadString = strSource 
End Function
BTW, we tested several options in a thread ages ago, but couldn't find it. Credit for the LSet is due to, without any surprise, strongm Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Another way. Use fixed length strings:
[tt]
Dim S As String * 10
S = &quot;ABC&quot;
'Now S holds &quot;ABC &quot;
[/tt]
 
There are many ways. Here is another one that I posted a few days ago.

Format$(&quot;ABC&quot;, &quot;!@@@@@@@@@@&quot;) Swi
 
The disadvantage of the last two methods is their inflexibility. The earlier solutions are more generic.
 
How is my example inflexible for the question originally asked. A few modifications and my example has almost all of the same flexibility as the other examples:


Function PadString(StrSource As String, PadLen As Long) As String
PadString = Format$(StrSource, &quot;!&quot; & String$(PadLen, &quot;@&quot;))
End Function Swi
 

Yes there are many ways but lets add some functionality and error checking (a repost from some time ago)...
[tt]
Option Explicit

Public Enum Side
PadLeftSide = 0
PadRightSide = 1
End Enum

Private Sub Form_Load()

Dim S As String, A As String
S = &quot;Test&quot;

A = Pad(S, 25, PadLeftSide)

End Sub

Public Function Pad(StringToPad As String, TotalLengthOfReturnString As Integer, LeftOrRight As Side, Optional CharacterToPadWith As String = &quot; &quot;) As String

On Error GoTo PadError

Dim StringLength As Integer, TotalLength As Integer, Difference As Integer

StringLength = Len(StringToPad)

Difference = TotalLengthOfReturnString - StringLength

If Difference > 0 Then

If LeftOrRight = Left Then

Pad = String(Difference, CharacterToPadWith) & StringToPad

Else

Pad = StringToPad & String(Difference, CharacterToPadWith)

End If

Else

Pad = StringToPad

End If

Exit Function
PadError:

MsgBox Err.Description

End Function
[/tt]

Just to add fuel to the fire (my .02) and to agree with strongm that there is more than one way.

:)

 
A little prickly there. How is it inflexible for the question originally asked? Well, because the original question asks how to generically pad a string. The 10 character element was merely an example.

Sure, you can rewrite it. And, as several people have said, there are multiple solutions to this problem, so it sure as heck isn't wrong. But it's still not as generic as the earlier solutions, even with the current rewrite. For example, what if PadLen is less than the length of strSource?
 
Swi,

It is a good method to use and usually flexible enough - if the pad char is a space.

If the pad character is not chr(32), then the format function will not work.
I have customers needing padded fields in an ascii file, and sometimes they need it padded with zeros.
 
FYI, I used the LSet function.
Thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top