Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. sprintf equivalent in c#

sprintf equivalent in c#

Scheduled Pinned Locked Moved C#
csharpdatabase
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kannan Kalyanaraman
    wrote on last edited by
    #1

    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

    C J 2 Replies Last reply
    0
    • K Kannan Kalyanaraman

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • K Kannan Kalyanaraman

        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

        J Offline
        J Offline
        James T Johnson
        wrote on last edited by
        #3

        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

        K 1 Reply Last reply
        0
        • J James T Johnson

          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

          K Offline
          K Offline
          Kannan Kalyanaraman
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • K Kannan Kalyanaraman

            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

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            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

            K 1 Reply Last reply
            0
            • J James T Johnson

              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

              K Offline
              K Offline
              Kannan Kalyanaraman
              wrote on last edited by
              #6

              Thanks a lot. I think I need to get a book first. Cheers Kannan

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups