parse a string to result in a specific format

Posted on 2007-07-24
Last Modified: 2010-04-15
I need to format this string...
Its in the code behind of a c# web app.
The string must always end up in the form of ##/##/####
Which is...2 numbers/2 numbers/4 numbers...
So I need to parse the string and if any of the 3 values are not the appropriate amount of digits, it needs to be preceeded with a zero, see below.
5/3/2341 should be 05/05/2341
12/2/1222 should be 12/01/1222
12/12/11 should be 12/12/0011

there should always be 2 digits/ 2 digits/ 4 digits
So I need to parse this string called valNumber to end up in this format. I know I need to use substring and some other methods but need the most effective way.
Question by:solraccheffy
    LVL 8

    Expert Comment

    LVL 8

    Expert Comment

    LVL 22

    Expert Comment

    Is the input three integers?

    Author Comment

    its a text box that has data entered, so by default its a string right?

    Author Comment

    I am getting this error message

    string start = this.txtstart.Text;
    start = start .ToString("##/##/####");

     cannot convert from 'string' to 'System.IFormatProvider'

    please advise
    LVL 22

    Accepted Solution

    Sorry - I didn't have my glasses on, and missed your example. So:
    string[] items = textBox1.Text.Split('/');
    string a1 = string.Format("{0}/{1}/{2}", items[0].PadLeft(2, '0'), items[1].PadLeft(2, '0'), items[2].PadLeft(4, '0'));

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now