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

Strings

Scheduled Pinned Locked Moved C#
tutorialquestion
6 Posts 6 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
    max29297
    wrote on last edited by
    #1

    Usually when I'm joining a bunch of things to make a string, I'll just do like string a = pt.X.ToString() + ", " + pt.Y.ToString();. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")


    I wish I could drive...


    P N G C P 5 Replies Last reply
    0
    • M max29297

      Usually when I'm joining a bunch of things to make a string, I'll just do like string a = pt.X.ToString() + ", " + pt.Y.ToString();. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")


      I wish I could drive...


      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #2

      I sometimes just use String.Format(....) ...

      "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

      1 Reply Last reply
      0
      • M max29297

        Usually when I'm joining a bunch of things to make a string, I'll just do like string a = pt.X.ToString() + ", " + pt.Y.ToString();. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")


        I wish I could drive...


        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        Because strings are immutable if there are several concatenations involved it is better to use StringBuilder[^] For small efforts I might consider String.Format for readability


        only two letters away from being an asset

        1 Reply Last reply
        0
        • M max29297

          Usually when I'm joining a bunch of things to make a string, I'll just do like string a = pt.X.ToString() + ", " + pt.Y.ToString();. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")


          I wish I could drive...


          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          For a single statement like that, there is no better method. The compiler turns that statement into: string a = String.Concat(pt.X.ToString(), ", ", pt.Y.ToString()); This will allocate a string buffer with the correct size and copies the data from each string to it, so it causes a minimum of overhead.

          Experience is the sum of all the mistakes you have done.

          1 Reply Last reply
          0
          • M max29297

            Usually when I'm joining a bunch of things to make a string, I'll just do like string a = pt.X.ToString() + ", " + pt.Y.ToString();. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")


            I wish I could drive...


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

            You should use a string builder

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            1 Reply Last reply
            0
            • M max29297

              Usually when I'm joining a bunch of things to make a string, I'll just do like string a = pt.X.ToString() + ", " + pt.Y.ToString();. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")


              I wish I could drive...


              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              I prefer string.Format for that. There was a thread on this a few months back.

              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