CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Pad a String

  1. #1
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    398

    Pad a String

    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)

    Leica Geosystems - when it has to be right

  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Pad a String

    not that I know of, I do write my own.

    HTH

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


  3. #3
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Pad a String

    A one line solution is
    Strng = "A"
    NewStr = StrReverse(Left$(Strng & String$(4, "0"), 4))




  4. #4
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Pad a String

    Sorry, a slight amendment
    Strng = "AB"
    NewStr = StrReverse(Left$(StrReverse(Strng) & String$(4, "0"), 4))




  5. #5
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Pad a String

    sString = "A"
    msgbox Format(sString,"0000") -> will give you "000A"

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  6. #6
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Pad a String

    You're always better...


  7. #7
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Pad a String

    Don't be shy. We all have something to learn from you.
    By the way welcome in VBCodeLibrary Forum.
    Good luck.

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  8. #8
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    398

    Re: Pad a String

    I have tried this, but get "A" and not "000A" ????

    Leica Geosystems - when it has to be right

  9. #9
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Pad a String

    Try
    msgbox Format$(sString,"0000")


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  10. #10
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Pad a String

    Anptherr solution

    Dim s As String
    Dim sLeadingZeroes As String

    sLeadingZeroes = "0000"
    s = "A"

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


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  11. #11
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    398

    Re: Pad a String

    Sorry - I have tried this, but still get "A" and not "000A" ?
    Does it work in your environment that way?

    Leica Geosystems - when it has to be right

  12. #12
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Pad a String

    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.
    ...at present time, using mainly Net 4.0, Vs 2010



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

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

Featured