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. Other Discussions
  3. The Weird and The Wonderful
  4. String.Format - I'm sure there must be a worse way to do this.

String.Format - I'm sure there must be a worse way to do this.

Scheduled Pinned Locked Moved The Weird and The Wonderful
hardware
18 Posts 10 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.
  • R Offline
    R Offline
    RCoate
    wrote on last edited by
    #1

    I'm a bit surprised there isn't an embedded String.Concatenate call in there as well.

    DateTime dt = DateTime.Now;

    // Get year, month, and day
    string year = dt.Year.ToString();
    string month = dt.Month.ToString();
    string day = dt.Day.ToString();

    // Format month
    if (month.Length == 1){ month = "0" + month; }

    // Format day
    if (day.Length == 1){ day = "0" + day; }

    string DocumentName = string.Format("Application - [{0}] - " + year + month + day, this.Person.RegistrationNo.ToString());

    I'm thinking that there is a ToString() call missing just before the final ; too. Oh - and this one too.

    TRIMSDK.Location trimLocation = trimLocation = trimManager.FindLocationFromNickname(string.Format("{0}", this.Person.RegistrationNo.GetValueOrDefault(0).ToString()));

    I think I need a Rum! :wtf:

    E M L 3 Replies Last reply
    0
    • R RCoate

      I'm a bit surprised there isn't an embedded String.Concatenate call in there as well.

      DateTime dt = DateTime.Now;

      // Get year, month, and day
      string year = dt.Year.ToString();
      string month = dt.Month.ToString();
      string day = dt.Day.ToString();

      // Format month
      if (month.Length == 1){ month = "0" + month; }

      // Format day
      if (day.Length == 1){ day = "0" + day; }

      string DocumentName = string.Format("Application - [{0}] - " + year + month + day, this.Person.RegistrationNo.ToString());

      I'm thinking that there is a ToString() call missing just before the final ; too. Oh - and this one too.

      TRIMSDK.Location trimLocation = trimLocation = trimManager.FindLocationFromNickname(string.Format("{0}", this.Person.RegistrationNo.GetValueOrDefault(0).ToString()));

      I think I need a Rum! :wtf:

      E Offline
      E Offline
      egenis
      wrote on last edited by
      #2

      RCoate wrote:

      // Format month if (month.Length == 1){ month = "0" + month; }   // Format day if (day.Length == 1){ day = "0" + day; }

      Now if that had been done with a StringBuilder... I would have been impressed! :-\

      R 1 Reply Last reply
      0
      • E egenis

        RCoate wrote:

        // Format month if (month.Length == 1){ month = "0" + month; }   // Format day if (day.Length == 1){ day = "0" + day; }

        Now if that had been done with a StringBuilder... I would have been impressed! :-\

        R Offline
        R Offline
        RCoate
        wrote on last edited by
        #3

        // Format month
        var sbMonth = new StringBuilder();
        if (month.Length == 1)
        {
        sbMonth.Append("0");
        }
        sbMonth.Append(month);

        Awesome! I knew there was another method missing! :)

        P 0 2 Replies Last reply
        0
        • R RCoate

          // Format month
          var sbMonth = new StringBuilder();
          if (month.Length == 1)
          {
          sbMonth.Append("0");
          }
          sbMonth.Append(month);

          Awesome! I knew there was another method missing! :)

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

          Now try it with Insert.

          R 1 Reply Last reply
          0
          • P PIEBALDconsult

            Now try it with Insert.

            R Offline
            R Offline
            RCoate
            wrote on last edited by
            #5

            Got your insert right here.

            // Format month
            var sbMonth = new StringBuilder();
            sbMonth.Append(month);
            if (month.Length == 1)
            {
            sbMonth.Insert(0, "0");
            }

            I'm sure if the genius who wrote this was aware of Insert and string builders, it would have been used. Can't complain too much. At least the code actually produces the required outcome. I can't always claim that. :)

            S 1 Reply Last reply
            0
            • R RCoate

              Got your insert right here.

              // Format month
              var sbMonth = new StringBuilder();
              sbMonth.Append(month);
              if (month.Length == 1)
              {
              sbMonth.Insert(0, "0");
              }

              I'm sure if the genius who wrote this was aware of Insert and string builders, it would have been used. Can't complain too much. At least the code actually produces the required outcome. I can't always claim that. :)

              S Offline
              S Offline
              Slacker007
              wrote on last edited by
              #6

              String.Remove() and String.Insert() - I use these a lot. BTW, I hardly use StringBuilder(), but that's just my style. ;)

              1 Reply Last reply
              0
              • R RCoate

                // Format month
                var sbMonth = new StringBuilder();
                if (month.Length == 1)
                {
                sbMonth.Append("0");
                }
                sbMonth.Append(month);

                Awesome! I knew there was another method missing! :)

                0 Offline
                0 Offline
                0bx
                wrote on last edited by
                #7

                var sbMonth = new StringBuilder();
                try
                {
                int x = 1 / month.Length -1;
                sbMonth = AppendMonth(sbMonth);
                }
                catch
                {
                sbMonth = AppendMonth(sbMonth, "0" + month);
                }

                private StringBuilder AppendMonth(StringBuilder sb, string s)
                {
                string x = sb.Append(s).ToString();
                StringBuilder y = new StringBuilder();
                return y.Append(x);
                }

                Giraffes are not real.

                R S 2 Replies Last reply
                0
                • 0 0bx

                  var sbMonth = new StringBuilder();
                  try
                  {
                  int x = 1 / month.Length -1;
                  sbMonth = AppendMonth(sbMonth);
                  }
                  catch
                  {
                  sbMonth = AppendMonth(sbMonth, "0" + month);
                  }

                  private StringBuilder AppendMonth(StringBuilder sb, string s)
                  {
                  string x = sb.Append(s).ToString();
                  StringBuilder y = new StringBuilder();
                  return y.Append(x);
                  }

                  Giraffes are not real.

                  R Offline
                  R Offline
                  RCoate
                  wrote on last edited by
                  #8

                  :omg: Yep. That's worse.

                  0 1 Reply Last reply
                  0
                  • 0 0bx

                    var sbMonth = new StringBuilder();
                    try
                    {
                    int x = 1 / month.Length -1;
                    sbMonth = AppendMonth(sbMonth);
                    }
                    catch
                    {
                    sbMonth = AppendMonth(sbMonth, "0" + month);
                    }

                    private StringBuilder AppendMonth(StringBuilder sb, string s)
                    {
                    string x = sb.Append(s).ToString();
                    StringBuilder y = new StringBuilder();
                    return y.Append(x);
                    }

                    Giraffes are not real.

                    S Offline
                    S Offline
                    Sentenryu
                    wrote on last edited by
                    #9

                    ok, now where's my mind bleach?

                    I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

                    B 1 Reply Last reply
                    0
                    • R RCoate

                      :omg: Yep. That's worse.

                      0 Offline
                      0 Offline
                      0bx
                      wrote on last edited by
                      #10

                      I'm so sorry... I just couldn't stop.

                      Giraffes are not real.

                      1 Reply Last reply
                      0
                      • R RCoate

                        I'm a bit surprised there isn't an embedded String.Concatenate call in there as well.

                        DateTime dt = DateTime.Now;

                        // Get year, month, and day
                        string year = dt.Year.ToString();
                        string month = dt.Month.ToString();
                        string day = dt.Day.ToString();

                        // Format month
                        if (month.Length == 1){ month = "0" + month; }

                        // Format day
                        if (day.Length == 1){ day = "0" + day; }

                        string DocumentName = string.Format("Application - [{0}] - " + year + month + day, this.Person.RegistrationNo.ToString());

                        I'm thinking that there is a ToString() call missing just before the final ; too. Oh - and this one too.

                        TRIMSDK.Location trimLocation = trimLocation = trimManager.FindLocationFromNickname(string.Format("{0}", this.Person.RegistrationNo.GetValueOrDefault(0).ToString()));

                        I think I need a Rum! :wtf:

                        M Offline
                        M Offline
                        Mario Majcica
                        wrote on last edited by
                        #11

                        You need to understand that a thing like this,

                        string DocumentName = string.Format("Application - [{0}] - {1:yyyyMMdd}", Person.RegistrationNo, DateTime.Now);

                        was not easy to understand and it could trick less experienced developers!!! Cheers!!!

                        B 1 Reply Last reply
                        0
                        • M Mario Majcica

                          You need to understand that a thing like this,

                          string DocumentName = string.Format("Application - [{0}] - {1:yyyyMMdd}", Person.RegistrationNo, DateTime.Now);

                          was not easy to understand and it could trick less experienced developers!!! Cheers!!!

                          B Offline
                          B Offline
                          BobJanova
                          wrote on last edited by
                          #12

                          Joke? That's far easier to read than what was originally posted.

                          M 1 Reply Last reply
                          0
                          • B BobJanova

                            Joke? That's far easier to read than what was originally posted.

                            M Offline
                            M Offline
                            Mario Majcica
                            wrote on last edited by
                            #13

                            Well it is flagged as a joke, also I hope my tone was sarcastic enough. Cheers

                            B 1 Reply Last reply
                            0
                            • M Mario Majcica

                              Well it is flagged as a joke, also I hope my tone was sarcastic enough. Cheers

                              B Offline
                              B Offline
                              BobJanova
                              wrote on last edited by
                              #14

                              Okay, just checking :)

                              1 Reply Last reply
                              0
                              • S Sentenryu

                                ok, now where's my mind bleach?

                                I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

                                B Offline
                                B Offline
                                Brisingr Aerowing
                                wrote on last edited by
                                #15

                                Here you go![^]

                                I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image. Stephen Hawking

                                S 1 Reply Last reply
                                0
                                • B Brisingr Aerowing

                                  Here you go![^]

                                  I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image. Stephen Hawking

                                  S Offline
                                  S Offline
                                  Sentenryu
                                  wrote on last edited by
                                  #16

                                  Thanks!

                                  I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

                                  B 1 Reply Last reply
                                  0
                                  • S Sentenryu

                                    Thanks!

                                    I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

                                    B Offline
                                    B Offline
                                    Brisingr Aerowing
                                    wrote on last edited by
                                    #17

                                    You're welcome!

                                    I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image. Stephen Hawking

                                    1 Reply Last reply
                                    0
                                    • R RCoate

                                      I'm a bit surprised there isn't an embedded String.Concatenate call in there as well.

                                      DateTime dt = DateTime.Now;

                                      // Get year, month, and day
                                      string year = dt.Year.ToString();
                                      string month = dt.Month.ToString();
                                      string day = dt.Day.ToString();

                                      // Format month
                                      if (month.Length == 1){ month = "0" + month; }

                                      // Format day
                                      if (day.Length == 1){ day = "0" + day; }

                                      string DocumentName = string.Format("Application - [{0}] - " + year + month + day, this.Person.RegistrationNo.ToString());

                                      I'm thinking that there is a ToString() call missing just before the final ; too. Oh - and this one too.

                                      TRIMSDK.Location trimLocation = trimLocation = trimManager.FindLocationFromNickname(string.Format("{0}", this.Person.RegistrationNo.GetValueOrDefault(0).ToString()));

                                      I think I need a Rum! :wtf:

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

                                      I would think this is how it's supposed to be done.

                                      string Daternator()
                                      {
                                      DateTime dt = DateTime.Now;

                                      string result = ""; // Failure to use String.Empty - Check
                                      
                                      // Homemade string concat - Check
                                      for (int i = 0; i < 3; i++)
                                      {
                                          if (i == 0)
                                              result += dt.Year.ToString() + ", ";
                                          else if (i == 1)
                                              result += dt.Month.ToString() + ", ";
                                          else if (i == 2)
                                              result += dt.Day.ToString() + ", "; // Unnecessary code - Check 
                                      }
                                      
                                      // More unnecessary code to fix previous unnecessary code - Check
                                      result = result.Remove(result.LastIndexOf(" "), 1);
                                      result = result.Remove(result.LastIndexOf(","), 1);
                                      
                                      // My lord, he wouldn't...
                                      Start:
                                      
                                      // And what's this? Regex? Fancy!
                                      MatchCollection mc = Regex.Matches(result, @"\\b(\\d)\\b");
                                      
                                      // I like where this is headed...
                                      foreach (Match m in mc)
                                      {
                                          if (m.Success)
                                          {
                                              result = result.Insert(m.Index, "0");
                                              goto Start; // Oh lord, he did, a goto loop - Check
                                          }
                                      }
                                      
                                      // Sweet sciency magic we have a result!!
                                      return string.Format("Application - \[{0}\] - " + result, this.Person.RegistrationNo.ToString());
                                      

                                      }

                                      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