I have a string format
and IEnumerable< object > parameters
(collection of boxed values) in my managed C# code.
format
- printf-like format specifier (according with ANSI/ISO 9899-1990),parameters
- collection of parameters (arguments) which need to process with the format
.
The task - how to get final formatted string from format specifier and collection of arguments? (except http://www.codeproject.com/KB/printing/PrintfImplementationinCS.aspx solution)
Please do not answer "Why you need printf format? Use String.Format
" or something like that... Unfortunately, I can not to escape from what I have...
No. printf is used in c programming c# is totally different from c. If you would like to use printf, you can make and overload methods to get the same results.
%s refers to a string %d refers to an integer %c refers to a character. Therefore: %s%d%s%c\n prints the string "The first character in sting ", %d prints i, %s prints " is ", and %c prints str[0].
In this program, we have used the printf() function to print the integer num and the C-string my_name . printf("num = %d \n", num); printf("My name is %s", my_name); Here, %d is replaced by the num variable in the output.
In C#, Format() is a string method. This method is used to replace one or more format items in the specified string with the string representation of a specified object.In other words, this method is used to insert the value of the variable or an object or expression into another string.
You have all the code for sprintf in code project article "A printf implementtion in C#" from Richard Prinz, modify it and compile it for your needs... Mainly the first few lines... Add that project to your own project or just the only one c# file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With