Search code examples
c++qtqstringqtcorechunks

Qt: Insert character every x characters


I am creating a little application in which I create a SHA1 hash with php and use the chunk_split function in php to insert a "|" every x characters.

Is there something similar? Or how would one split the string and insert a character there?

PHP Code:

substr(strtoupper(chunk_split(sha1("this is my super secure test application"), 5, "-")), 0, 29);

How can I do this with Qt?


Solution

  • You could write something like this:

    main.cpp

    #include <QString>
    #include <QDebug>
    
    int main()
    {
        const int step = 3;
        const char mychar = 'x';
        QString myString = "FooBarBaz";
        for (int i = step; i <= myString.size(); i+=step+1)
            myString.insert(i, mychar);
        qDebug() << myString;
        return 0;
    }
    

    main.pro

    TEMPLATE = app
    TARGET = main
    QT = core
    SOURCES += main.cpp
    

    Build and Run

    qmake && make && ./main
    

    Output

    "FooxBarxBazx"
    

    See the documentation of the insert method in here.