Results 1 to 4 of 4

Thread: VB2008 fixed string and fill the rest with zeros

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Posts
    134

    VB2008 fixed string and fill the rest with zeros

    I need to format a string in a fixed number of characters.

    For example, if someone type TWO

    It has too save in the variable

    000TWO

    if type THREE

    0THREE

    Always 6 characters and fill the rest with zeros on the left.

    Any idea?

  2. #2
    Hyperactive Member BadgerBadger's Avatar
    Join Date
    Aug 2009
    Location
    Wales
    Posts
    382

    Re: VB2008 fixed string and fill the rest with zeros

    Subtract the fixed number of characters from the length of the string the user inputs.
    Add the zeros that many times beforehand.

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: VB2008 fixed string and fill the rest with zeros

    You can use the PadLeft function:

    Code:
            Dim s As String = "TWO"
            s = s.PadLeft(6, "0"c)
            MessageBox.Show(s)
    Last edited by Negative0; Sep 16th, 2009 at 03:09 PM. Reason: Fixed option strict problem. How'd that get turned off?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Posts
    134

    Re: VB2008 fixed string and fill the rest with zeros

    Thanks Negative0, I didn't know about the padleft function. worked perfectly

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