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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Formatting Strings

Formatting Strings

Scheduled Pinned Locked Moved C#
question
9 Posts 5 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.
  • M Offline
    M Offline
    Maharishi Bhatia
    wrote on last edited by
    #1

    Hi all Can anyone suggest me which way of string replacment is much better ?? 1) string str = "Date today is " + Date.Now.ToString() + " !!!!" Or 2) string str = String.Format("Date today is {0} !!!!",Date.Now.ToString()) Thanxs Maharishi Bhatia

    H A 2 Replies Last reply
    0
    • M Maharishi Bhatia

      Hi all Can anyone suggest me which way of string replacment is much better ?? 1) string str = "Date today is " + Date.Now.ToString() + " !!!!" Or 2) string str = String.Format("Date today is {0} !!!!",Date.Now.ToString()) Thanxs Maharishi Bhatia

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Never concatenate strings using + - it's very expensive since two iterations for each string and each concatenation are necessary, making it an (m-1)O(2n) operation where n is the number of character for two strings and m is the number of strings. Use String.Concat or String.Format like you're doing, but don't use ToString. What do you thing String.Format is for? Lets say you wanted to format the date using the long date format. Just use the following:

      string s = string.Format("The date is {0:D}.", DateTime.Now);

      If you use ToString, you loose that ability (unless you use the overload for DateTime.ToString which takes a string and optionally a different culture's IFormatProvider. I'll be submitting an article on this topic in the next couple days. You might want to keep a look out and read it.

      Microsoft MVP, Visual C# My Articles

      S J 2 Replies Last reply
      0
      • H Heath Stewart

        Never concatenate strings using + - it's very expensive since two iterations for each string and each concatenation are necessary, making it an (m-1)O(2n) operation where n is the number of character for two strings and m is the number of strings. Use String.Concat or String.Format like you're doing, but don't use ToString. What do you thing String.Format is for? Lets say you wanted to format the date using the long date format. Just use the following:

        string s = string.Format("The date is {0:D}.", DateTime.Now);

        If you use ToString, you loose that ability (unless you use the overload for DateTime.ToString which takes a string and optionally a different culture's IFormatProvider. I'll be submitting an article on this topic in the next couple days. You might want to keep a look out and read it.

        Microsoft MVP, Visual C# My Articles

        S Offline
        S Offline
        Spanky3
        wrote on last edited by
        #3

        bloody hell never knew it was expensive to use ToString() ! Well you learn something everyday

        H 1 Reply Last reply
        0
        • S Spanky3

          bloody hell never knew it was expensive to use ToString() ! Well you learn something everyday

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          It isn't expensive to use ToString (unless the implementation is crappy), I said it was expensive to concat strings using +. Using ToString in the variable argument list for String.Format (and the like) is not only unnecessary but in many cases dumb since you loose the ability to apply custom formats (unless you pass the format specifiers as args to the ToString overload for the IFormattable implementation).

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          • H Heath Stewart

            Never concatenate strings using + - it's very expensive since two iterations for each string and each concatenation are necessary, making it an (m-1)O(2n) operation where n is the number of character for two strings and m is the number of strings. Use String.Concat or String.Format like you're doing, but don't use ToString. What do you thing String.Format is for? Lets say you wanted to format the date using the long date format. Just use the following:

            string s = string.Format("The date is {0:D}.", DateTime.Now);

            If you use ToString, you loose that ability (unless you use the overload for DateTime.ToString which takes a string and optionally a different culture's IFormatProvider. I'll be submitting an article on this topic in the next couple days. You might want to keep a look out and read it.

            Microsoft MVP, Visual C# My Articles

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            Heath Stewart wrote: Never concatenate strings using + ...Use String.Concat C# code that 'pluses' strings together gets IL compiled down to .Concat, right? Maybe I'm misunderstanding you. --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

            H 1 Reply Last reply
            0
            • J Judah Gabriel Himango

              Heath Stewart wrote: Never concatenate strings using + ...Use String.Concat C# code that 'pluses' strings together gets IL compiled down to .Concat, right? Maybe I'm misunderstanding you. --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              For the Microsoft C# compiler, yes, and under certain circumstances - which typically warrant String.Format for better control. Depending on the operands you're passing, the compiler doesn't always optimize using String.Concat either.

              Microsoft MVP, Visual C# My Articles

              A 1 Reply Last reply
              0
              • H Heath Stewart

                For the Microsoft C# compiler, yes, and under certain circumstances - which typically warrant String.Format for better control. Depending on the operands you're passing, the compiler doesn't always optimize using String.Concat either.

                Microsoft MVP, Visual C# My Articles

                A Offline
                A Offline
                Alvaro Mendez
                wrote on last edited by
                #7

                Well then, if you take into account that it's almost always optimized into String.Concat, then wouldn't you say that using + is actually much quicker than using String.Format, which involves parsing and appending? From a readibility stand point, the + operator is superior in my opinion. Now, if special formatting is necessary, then the Format method is available -- for formatting purposes. Regards, Alvaro


                Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.

                H 1 Reply Last reply
                0
                • M Maharishi Bhatia

                  Hi all Can anyone suggest me which way of string replacment is much better ?? 1) string str = "Date today is " + Date.Now.ToString() + " !!!!" Or 2) string str = String.Format("Date today is {0} !!!!",Date.Now.ToString()) Thanxs Maharishi Bhatia

                  A Offline
                  A Offline
                  Alvaro Mendez
                  wrote on last edited by
                  #8
                  1. string str = "Date today is " + Date.Now + " !!!!"; ToString() is automatically called for you. :-) Regards, Alvaro

                  Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.

                  1 Reply Last reply
                  0
                  • A Alvaro Mendez

                    Well then, if you take into account that it's almost always optimized into String.Concat, then wouldn't you say that using + is actually much quicker than using String.Format, which involves parsing and appending? From a readibility stand point, the + operator is superior in my opinion. Now, if special formatting is necessary, then the Format method is available -- for formatting purposes. Regards, Alvaro


                    Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.

                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #9

                    It depends on the circumstances, I'd say. Like you said, for special formatting using String.Format is definitely better, though you could accomplish this task with + or String.Concat if you use the overloaded ToString that corresponds to the IFormattable implementation.

                    Microsoft MVP, Visual C# My Articles

                    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