locked
Is there any built in method to add leading zeros to string? RRS feed

  • Question

  • Hi i am working with c# windows application

    I dot know how many zero to add exactly.

    Is there any method to give the direct value.

    MyFormat=ddlCounter.Text(Ex:-00001)
    MyStartNo=TxtStart.Text(Ex:-10)

    I want output in some string ex:-strStartno=00010

    Is there any direct method to add leading zeros depending on dropdown select value and the textbox value.

    Regards,
    venkat.


    venakt
    Tuesday, November 25, 2008 12:27 PM

Answers

  • Hi,

    Didn't the the last reply of mine do that trick?
    Or is there some problems?

    Tomi Airaksinen - MCPD [Remember to click "mark as answered" when you get a correct reply to your question]
    • Marked as answer by Zhi-Xin Ye Monday, December 1, 2008 8:10 AM
    Tuesday, November 25, 2008 12:34 PM
  •  The PadLeft method allows this sort of thing.

    Dim yourString As String

    yourString = "1000"

    yourString.PadLeft(4, CChar("0"))


    This will give a string of "00001000"

    The above will put 4 zero characters on the front of your string.

    Hope this helps.

    • Proposed as answer by Jon Mitchell Tuesday, November 25, 2008 12:37 PM
    • Marked as answer by Zhi-Xin Ye Monday, December 1, 2008 8:10 AM
    Tuesday, November 25, 2008 12:35 PM

All replies

  • Hi,

    Didn't the the last reply of mine do that trick?
    Or is there some problems?

    Tomi Airaksinen - MCPD [Remember to click "mark as answered" when you get a correct reply to your question]
    • Marked as answer by Zhi-Xin Ye Monday, December 1, 2008 8:10 AM
    Tuesday, November 25, 2008 12:34 PM
  •  The PadLeft method allows this sort of thing.

    Dim yourString As String

    yourString = "1000"

    yourString.PadLeft(4, CChar("0"))


    This will give a string of "00001000"

    The above will put 4 zero characters on the front of your string.

    Hope this helps.

    • Proposed as answer by Jon Mitchell Tuesday, November 25, 2008 12:37 PM
    • Marked as answer by Zhi-Xin Ye Monday, December 1, 2008 8:10 AM
    Tuesday, November 25, 2008 12:35 PM