Click to See Complete Forum and Search --> : Pad a String


Markus W.
June 13th, 2001, 09:26 AM
Ok, I can write it my own, but is there a VB function to 'left pad' a string?

e.g. "A" -> "000A"

something like: LeftPad("A","0",4)

cksiow
June 13th, 2001, 09:33 AM
not that I know of, I do write my own.

HTH

cksiow
http://vblib.virtualave.net - share our codes

shree
June 13th, 2001, 09:36 AM
A one line solution is
Strng = "A"
NewStr = StrReverse(Left$(Strng & String$(4, "0"), 4))

shree
June 13th, 2001, 09:38 AM
Sorry, a slight amendment
Strng = "AB"
NewStr = StrReverse(Left$(StrReverse(Strng) & String$(4, "0"), 4))

Iouri
June 13th, 2001, 09:46 AM
sString = "A"
msgbox Format(sString,"0000") -> will give you "000A"

Iouri Boutchkine
iouri@hotsheet.com

shree
June 13th, 2001, 09:48 AM
You're always better...

Iouri
June 13th, 2001, 10:39 AM
Don't be shy. We all have something to learn from you.
By the way welcome in VBCodeLibrary Forum.
Good luck.

Iouri Boutchkine
iouri@hotsheet.com

Markus W.
June 14th, 2001, 01:13 AM
I have tried this, but get "A" and not "000A" ????

Iouri
June 15th, 2001, 02:22 PM
Try
msgbox Format$(sString,"0000")


Iouri Boutchkine
iouri@hotsheet.com

Iouri
June 15th, 2001, 02:38 PM
Anptherr solution

Dim s As String
Dim sLeadingZeroes As String

sLeadingZeroes = "0000"
s = "A"

MsgBox Left(sLeadingZeroes, Len(sLeadingZeroes) - Len(s)) & s


Iouri Boutchkine
iouri@hotsheet.com

Markus W.
June 18th, 2001, 12:45 AM
Sorry - I have tried this, but still get "A" and not "000A" ?
Does it work in your environment that way?

Cimperiali
June 18th, 2001, 03:13 AM
Try This:

Dim mytext as string * 4
RSet mytext = Text1.Text
mytext = Replace(mytext, " ", "0")
Text1.Text = mytext




Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.