Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I append characters of a QString to another QString in a neat way?

How do I append characters of a QString to another QString in a neat way?

Scheduled Pinned Locked Moved Solved General and Desktop
timeqstringappending
5 Posts 4 Posters 1.4k Views
  • 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.
  • L Offline
    L Offline
    legitnameyo
    wrote on 9 Jul 2019, 03:42 last edited by legitnameyo 7 Sept 2019, 03:50
    #1

    I've got the following code and it doesn't really look all that pretty to me

    QString current_t = QDateTime::currentDateTime().toString("yyyyMMddhmmss"); // the current time and date
    
    QString  yyyy, MM, dd, h, min, sec = ""; // years, month, day, hour, minutes, seconds
    
            if(current_t.length() == 13) { // used to make the time for the hour to e.g. 05 instead of just 5
                current_t.insert(8, "0");
            }
            // year
            yyyy += current_t[0];
            yyyy += current_t[1];
            yyyy += current_t[2];
            yyyy += current_t[3];
            // month        
            MM += current_t[4];
            MM += current_t[5];
            // day
            dd += current_t[6];
            dd += current_t[7];
            // hour
            h += current_t[8];
            h += current_t[9];
            // minute
            min += current_t[10];
            min += current_t[11];
            // second
            sec += current_t[12];
            sec += current_t[13];
    

    is there any more efficient way of doing this?

    J J 2 Replies Last reply 9 Jul 2019, 04:22
    0
    • L legitnameyo
      9 Jul 2019, 03:42

      I've got the following code and it doesn't really look all that pretty to me

      QString current_t = QDateTime::currentDateTime().toString("yyyyMMddhmmss"); // the current time and date
      
      QString  yyyy, MM, dd, h, min, sec = ""; // years, month, day, hour, minutes, seconds
      
              if(current_t.length() == 13) { // used to make the time for the hour to e.g. 05 instead of just 5
                  current_t.insert(8, "0");
              }
              // year
              yyyy += current_t[0];
              yyyy += current_t[1];
              yyyy += current_t[2];
              yyyy += current_t[3];
              // month        
              MM += current_t[4];
              MM += current_t[5];
              // day
              dd += current_t[6];
              dd += current_t[7];
              // hour
              h += current_t[8];
              h += current_t[9];
              // minute
              min += current_t[10];
              min += current_t[11];
              // second
              sec += current_t[12];
              sec += current_t[13];
      

      is there any more efficient way of doing this?

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 9 Jul 2019, 04:22 last edited by JKSH 7 Sept 2019, 04:36
      #2

      @legitnameyo said in How do I append characters of a QString to another QString in a neat way?:

          if(current_t.length() == 13) { // used to make the time for the hour to e.g. 05 instead of just 5
              current_t.insert(8, "0");
          }
      

      If you want 05 instead of 5, then use hh or HH. Don't use h.

      QDateTime::currentDateTime().toString("yyyyMMddHHmmss")

         // year
         yyyy += current_t[0];
         yyyy += current_t[1];
         yyyy += current_t[2];
         yyyy += current_t[3];
      

      See https://doc.qt.io/qt-5/qstring.html#mid : yyyy = current_t.mid(0, 4);

      QString  yyyy, MM, dd, h, min, sec = "";
      

      Don't initialize a QString with = "".

      QString yyyy1 = ""; // Bad. Wastes CPU cycles.
      
      QString yyyy2;      // Good. It is automatically initialized to a null string
      

      Anyway, you could simplify the whole thing:

      QDateTime now = QDateTime::currentDateTime();
      
      QString yyyy = now.toString("yyyy");
      QString MM = now.toString("MM");
      QString dd = now.toString("dd");
      QString HH = now.toString("HH");
      QString mm = now.toString("mm");
      QString ss = now.toString("ss");
      

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      K 1 Reply Last reply 9 Jul 2019, 04:31
      5
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 9 Jul 2019, 04:22 last edited by
        #3

        there are many better ways to do this:

        1. use regular expressions to parse the date/time string
        2. QString::mid() method
          .
          .
          .
        1 Reply Last reply
        2
        • L legitnameyo
          9 Jul 2019, 03:42

          I've got the following code and it doesn't really look all that pretty to me

          QString current_t = QDateTime::currentDateTime().toString("yyyyMMddhmmss"); // the current time and date
          
          QString  yyyy, MM, dd, h, min, sec = ""; // years, month, day, hour, minutes, seconds
          
                  if(current_t.length() == 13) { // used to make the time for the hour to e.g. 05 instead of just 5
                      current_t.insert(8, "0");
                  }
                  // year
                  yyyy += current_t[0];
                  yyyy += current_t[1];
                  yyyy += current_t[2];
                  yyyy += current_t[3];
                  // month        
                  MM += current_t[4];
                  MM += current_t[5];
                  // day
                  dd += current_t[6];
                  dd += current_t[7];
                  // hour
                  h += current_t[8];
                  h += current_t[9];
                  // minute
                  min += current_t[10];
                  min += current_t[11];
                  // second
                  sec += current_t[12];
                  sec += current_t[13];
          

          is there any more efficient way of doing this?

          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 9 Jul 2019, 04:25 last edited by jsulm 7 Sept 2019, 04:25
          #4

          @legitnameyo said in How do I append characters of a QString to another QString in a neat way?:

          is there any more efficient way of doing this?

          Yes, use QString::mid, https://doc.qt.io/qt-5/qstring.html#mid

          QString  yyyy = current_t.mid(0, 4);
          QString MM = current_t.mid(3, 2);
          ...
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4
          • J JKSH
            9 Jul 2019, 04:22

            @legitnameyo said in How do I append characters of a QString to another QString in a neat way?:

                if(current_t.length() == 13) { // used to make the time for the hour to e.g. 05 instead of just 5
                    current_t.insert(8, "0");
                }
            

            If you want 05 instead of 5, then use hh or HH. Don't use h.

            QDateTime::currentDateTime().toString("yyyyMMddHHmmss")

               // year
               yyyy += current_t[0];
               yyyy += current_t[1];
               yyyy += current_t[2];
               yyyy += current_t[3];
            

            See https://doc.qt.io/qt-5/qstring.html#mid : yyyy = current_t.mid(0, 4);

            QString  yyyy, MM, dd, h, min, sec = "";
            

            Don't initialize a QString with = "".

            QString yyyy1 = ""; // Bad. Wastes CPU cycles.
            
            QString yyyy2;      // Good. It is automatically initialized to a null string
            

            Anyway, you could simplify the whole thing:

            QDateTime now = QDateTime::currentDateTime();
            
            QString yyyy = now.toString("yyyy");
            QString MM = now.toString("MM");
            QString dd = now.toString("dd");
            QString HH = now.toString("HH");
            QString mm = now.toString("mm");
            QString ss = now.toString("ss");
            
            K Offline
            K Offline
            Kent-Dorfman
            wrote on 9 Jul 2019, 04:31 last edited by
            #5

            @JKSH said in How do I append characters of a QString to another QString in a neat way?:

            Anyway, you could simplify the whole thing:
            QDateTime now = QDateTime::currentDateTime();

            QString yyyy = now.toString("yyyy");
            QString MM = now.toString("MM");
            QString dd = now.toString("dd");
            QString HH = now.toString("HH");
            QString mm = now.toString("mm");
            QString ss = now.toString("ss");

            This is ideally the best general solution, IMHO

            1 Reply Last reply
            1

            5/5

            9 Jul 2019, 04:31

            • Login

            • Login or register to search.
            5 out of 5
            • First post
              5/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved