sprintf equivalent in c#
-
Can someone tell me whats the sprintf equivalent in c#. I want to format a string, an sql statement (insert into ...) based on some values. thanks Kannan
-
Can someone tell me whats the sprintf equivalent in c#. I want to format a string, an sql statement (insert into ...) based on some values. thanks Kannan
There is nothing as evil as sprintf in C#, but a facility similar to ostringstream is StringBuilder. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
-
Can someone tell me whats the sprintf equivalent in c#. I want to format a string, an sql statement (insert into ...) based on some values. thanks Kannan
String.Format it takes the same arguments that Console.WriteLine does which allows you to specify some formatting options similar to ?printf. Here is a link to the format string specification. HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
-
String.Format it takes the same arguments that Console.WriteLine does which allows you to specify some formatting options similar to ?printf. Here is a link to the format string specification. HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
Thanks. I have another question. Whenever I use a 'String' type in c# most of the times I end up calling the member functions directly without creating any instance are they static methods(for ex. System.String.Format), which in turn return a 'string' type. 'string' is the simple type - does this encapsulate the 'String' type of the framework. Also, I understand I dont have to do a 'new' to access the 'string' types while the 'String' requires a 'new'. Is that true that variables of type 'string' is created in the stack and the 'String' is created in the heap. regards Kannan
-
Thanks. I have another question. Whenever I use a 'String' type in c# most of the times I end up calling the member functions directly without creating any instance are they static methods(for ex. System.String.Format), which in turn return a 'string' type. 'string' is the simple type - does this encapsulate the 'String' type of the framework. Also, I understand I dont have to do a 'new' to access the 'string' types while the 'String' requires a 'new'. Is that true that variables of type 'string' is created in the stack and the 'String' is created in the heap. regards Kannan
Kannan Kalyanaraman wrote: 'string' is the simple type - does this encapsulate the 'String' type of the framework. string is the C# name for .NET's String type, same for int/Int32, byte/Byte, char/Char, etc. In each case its just a different capitalization for the Framework name, everything else is the same :) Kannan Kalyanaraman wrote: Is that true that variables of type 'string' is created in the stack and the 'String' is created in the heap. Nope, String is a reference type so it is created on the heap. But strings can made Interned since I can't explain it very where here is a copy 'n paste from the docs.
The common language runtime automatically maintains a table, called the "intern pool", which contains a single instance of each unique literal string constant declared in a program, as well as any unique instance of String you add programmatically.
The intern pool conserves string storage. If you assign a literal string constant to several variables, each variable is set to reference the same constant in the intern pool instead of referencing several different instances of String that have identical values.
This might be what you are thinking of instead of the stack/heap :) James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978 -
Kannan Kalyanaraman wrote: 'string' is the simple type - does this encapsulate the 'String' type of the framework. string is the C# name for .NET's String type, same for int/Int32, byte/Byte, char/Char, etc. In each case its just a different capitalization for the Framework name, everything else is the same :) Kannan Kalyanaraman wrote: Is that true that variables of type 'string' is created in the stack and the 'String' is created in the heap. Nope, String is a reference type so it is created on the heap. But strings can made Interned since I can't explain it very where here is a copy 'n paste from the docs.
The common language runtime automatically maintains a table, called the "intern pool", which contains a single instance of each unique literal string constant declared in a program, as well as any unique instance of String you add programmatically.
The intern pool conserves string storage. If you assign a literal string constant to several variables, each variable is set to reference the same constant in the intern pool instead of referencing several different instances of String that have identical values.
This might be what you are thinking of instead of the stack/heap :) James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978Thanks a lot. I think I need to get a book first. Cheers Kannan