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



  • 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?


  • Moderators

    @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");
    


  • there are many better ways to do this:

    1. use regular expressions to parse the date/time string
    2. QString::mid() method
      .
      .
      .

  • Qt Champions 2018

    @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);
    ...
    


  • @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