Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf format in .NET

Tags:

c#

printf

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...

like image 677
Kirill Rafalson Avatar asked Nov 29 '11 05:11

Kirill Rafalson


People also ask

Can you use printf in C#?

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.

What is %d %f %s in c?

%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].

Can we use %d in printf?

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.

What is format in C#?

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.


1 Answers

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.

like image 57
Eric Ouellet Avatar answered Oct 19 '22 18:10

Eric Ouellet