• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

parse a string to result in a specific format

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.
0
solraccheffy
Asked:
solraccheffy
  • 2
  • 2
  • 2
1 Solution
 
sabeeshCommented:
test.ToString("##/##/####")
0
 
JimBrandleyCommented:
Is the input three integers?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
solraccheffyAuthor Commented:
its a text box that has data entered, so by default its a string right?
0
 
solraccheffyAuthor Commented:
I am getting this error message

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

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

please advise
0
 
JimBrandleyCommented:
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'));
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now