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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pad Right

Status
Not open for further replies.

Lambro

Programmer
Aug 22, 2001
258
US
I have a function which puts zero's in front of my data. The number of zero's is based of the length I specify. Here's the function:


Function Pad(strText, nLen, strChar, bFront)
Dim nStartLen
If strChar = "" Then
strChar = "0"
End If
nStartLen = Len(strText)
If Len(strText) >= nLen Then
Pad = strText
Else
If bFront Then
Pad = String(nLen - Len(strText), strChar) & strText
Else
Pad = strText & String(nLen - Len(strText), strChar)
End If
End If
End Function





Here's the code that Pad's the zero's in front.


iCheckNumber = Pad(Replace(CStr(Request.Form("CheckNumber")), ".", ""), 9, "0", True)



so if my check number entered in the form was 243 then the Pad function would add the zero's to make it look like the following: 000000243

What I want is for the first six zero's to be blank spaces and then 243 so it would be like " 243" without the ""

 
Use this:
iCheckNumber = Pad(Replace(CStr(Request.Form("CheckNumber")), ".", ""), 9, " ", True)


[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
or better:
If strChar = "" Then
strChar = " "
End If


Known is handfull, Unknown is worldfull
 
HTML will only display one space regardless of how many are in the string so;
Code:
response.write ShowSpaces(PadSpacesToLen("str",9,true))
or
Code:
response.write ShowSpaces(PadSpaces("str",9,true))

Code:
function PadSpaces(strIn,intPadNum,bFront)
if bFront then
     PadSpaces = space(intPadNum) & strIn
else
     PadSpaces = strIn & space(intPadNum)
end if
end function

function PadSpacesToLen(strIn,intLen,bFront)
intPadNum = intLen - Len(strIn)
if bFront then
     PadSpacesToLen = space(intPadNum) & strIn
else
     PadSpacesToLen = strIn & space(intPadNum)
end if
end function

function ShowSpaces(strIn)
ShowSpaces = replace(strIn," "," ")
end function





Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
This is what I had:

iCheckNumber = Pad(Replace(CStr(Request.Form("CheckNumber")), ".", ""), 9, "0", True)


This works perfectly:

iCheckNumber = Pad(Replace(CStr(Request.Form("CheckNumber")), ".", ""), 9, " ", False)


All I had to do was replace the "0" with " "
And True to False
 
if your function is still being grumpy.. and if you'd like to be able to use zeros instead of spaces

Code:
Function Padder(Source,PaddedLen,PadChar,PadFront)
    If Len(PadChar) > 1 Then Exit Function
    If PadChar = "" Then
        PadChar = " "
    End If
    If IsNull(PadFront) Then
        PadFront = False ' defaults to padding the right or back side.
    End If
    Padder = Source
    For PadPos = Len(Source) To PaddedLen - 1
        If Not PadFront Then
            Padder = Padder & PadChar
        Else
            Padder = PadChar & Padder
        End If
    Next
End Function

[thumbsup2]DreX
aKa - Robert
 
Argh, for loop, bleh ;)
Plus you have an error condition in there...

The original function is still probably a little faster (using the String() method).

-T

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top