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. Padding a string with integers on its right keeping string length fixed

Padding a string with integers on its right keeping string length fixed

Scheduled Pinned Locked Moved C#
questioncsharptutorial
15 Posts 6 Posters 16 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.
  • realJSOPR realJSOP

    string x = string.Format("T{0}", number.ToString("00000"));

    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
    -----
    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
    -----
    When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #6

    Even simpler:

    string x = string.Format("T{0:D5}", number);

    Or:

    string x = $"T{number:D5}";


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    realJSOPR 1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      Even simpler:

      string x = string.Format("T{0:D5}", number);

      Or:

      string x = $"T{number:D5}";


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #7

      isn't the "$" notation for PHP or some other stupid language? Or is it a new pointless operator in C#?

      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
      -----
      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
      -----
      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

      Richard DeemingR 1 Reply Last reply
      0
      • realJSOPR realJSOP

        isn't the "$" notation for PHP or some other stupid language? Or is it a new pointless operator in C#?

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #8

        It's been a C# feature since v6: $ - string interpolation - C# reference | Microsoft Docs[^] What's New in C# 6 - C# Guide | Microsoft Docs[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        realJSOPR 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          It's been a C# feature since v6: $ - string interpolation - C# reference | Microsoft Docs[^] What's New in C# 6 - C# Guide | Microsoft Docs[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #9

          Let's obfuscate C# even more than it already is...

          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
          -----
          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
          -----
          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

          Richard DeemingR 1 Reply Last reply
          0
          • realJSOPR realJSOP

            Let's obfuscate C# even more than it already is...

            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
            -----
            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
            -----
            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #10

            String interpolation was a pretty good addition, IMO. Especially when you compare it to the abomination that became of default interface methods[^]. X|


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            OriginalGriffO 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              String interpolation was a pretty good addition, IMO. Especially when you compare it to the abomination that became of default interface methods[^]. X|


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #11

              I agree - I use string interpolation precisely because it de-obfuscates code:

              Console.WriteLine("({2},{1}):({0},{3})={4}", destX, y, x, destY, dist);

              Isn't as obvious as

              Console.WriteLine($"({x},{y}):({destX},{destY})={dist}");

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              L 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                I agree - I use string interpolation precisely because it de-obfuscates code:

                Console.WriteLine("({2},{1}):({0},{3})={4}", destX, y, x, destY, dist);

                Isn't as obvious as

                Console.WriteLine($"({x},{y}):({destX},{destY})={dist}");

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #12

                OriginalGriff wrote:

                precisely because it de-obfuscates code:

                Except when you use random ordering of your variables. ;P

                OriginalGriffO 1 Reply Last reply
                0
                • L Lost User

                  OriginalGriff wrote:

                  precisely because it de-obfuscates code:

                  Except when you use random ordering of your variables. ;P

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #13

                  :laugh: I've seen code like that before: when the variable list order is wrong, and it was easier to renumber than juggle the names ... Not that I'd write code like that, oh no.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  realJSOPR D 2 Replies Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    :laugh: I've seen code like that before: when the variable list order is wrong, and it was easier to renumber than juggle the names ... Not that I'd write code like that, oh no.

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #14

                    For me, the numbers have to be in order, so I juggle the names. :)

                    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                    -----
                    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                    -----
                    When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      :laugh: I've seen code like that before: when the variable list order is wrong, and it was easier to renumber than juggle the names ... Not that I'd write code like that, oh no.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                      D Offline
                      D Offline
                      Daniel Pfeffer
                      wrote on last edited by
                      #15

                      A legitimate use case for unordered variables might be when writing output in different languages. In one language you might want to write "Page 1 of 10", but in another it might be "10 pages, No. 1" or some such. You would read the template from the resource file, filling in the values as necessary.

                      Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                      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