Answered by:
sprintf() equivalent in C#

Question
-
C++:
sprintf(formattedDate, "%02d/%02d/%02d", Month, Day, Year);
C#:
public char[] formattedDate = new char[255];
public int Month;
public int Day;
public int Year;string.Format(formattedDate, "%02d/%02d/%02d", Month, Day, Year);
I tried the above C# code but it throws error. Need a fix.
Monday, February 23, 2015 7:32 AM
Answers
-
The string.Format method in C# returs a string that can store directly in a string variable:
string strFormattedDate = string.Format("{0}{1}{2}", Month, Day, Year);
Each format item to be replace by the argument(s) is specified using the {x} syntax where x is the index of the parameter to be inserted: https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx
Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question.
- Proposed as answer by Franklin ChenMicrosoft employee Tuesday, February 24, 2015 1:51 AM
- Marked as answer by Franklin ChenMicrosoft employee Monday, March 2, 2015 11:55 AM
Monday, February 23, 2015 8:55 AM
All replies
-
Perhaps you should explain what you're trying to do.
For those of us who don't know c++ well enough to guess.
Oh.
And you posted to the wpf forum.
There's a c# forum which would have been more appropriate.
I used to be a points hound.
But I'm alright nooooooooooooooooooooooooooooOOOOOWWWW !Monday, February 23, 2015 7:48 AM -
Read the docs; https://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
http://pauliom.wordpress.com
Monday, February 23, 2015 7:49 AM -
The string.Format method in C# returs a string that can store directly in a string variable:
string strFormattedDate = string.Format("{0}{1}{2}", Month, Day, Year);
Each format item to be replace by the argument(s) is specified using the {x} syntax where x is the index of the parameter to be inserted: https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx
Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question.
- Proposed as answer by Franklin ChenMicrosoft employee Tuesday, February 24, 2015 1:51 AM
- Marked as answer by Franklin ChenMicrosoft employee Monday, March 2, 2015 11:55 AM
Monday, February 23, 2015 8:55 AM -
That's "ok" but you may want the data to be formatted as a specific number type, in which case you need the addition syntax, similar to printf.
http://pauliom.wordpress.com
Monday, February 23, 2015 12:32 PM