Remove dashes of string

  • Good afternoon.
    There is a line that is displayed in the line. When forming the result, double dashes or triple dashes can remain as at the beginning and at the end. How can I remove them? Or can a string form another?

        const QString format = "ABC-%0-%1%2-%3-%4%5-%6-%7-%8-%9-%10-%11";
        return format
                .arg(data_[highLimit])     // 
                .arg(data_[pressure])      // 
                .arg(data_[modification])  //
                .arg(data_[3])             //
                .arg(data_[4])             // 
                .arg(data_[5])             // 
                .arg(data_[6])             //
                .arg(data_[7])             // 
                .arg(data_[8])             // 
                .arg(data_[9])             // 
                .arg(data_[10])            // 

    result : ABC-30-40М2-1-1КТ-S--КО---

  • @fender

    result : ABC-30-40М2-1-1КТ-S--КО---

    What precisely would you like this returned string to be? Double- or triple-dashes (only) completely removed? Replaced by a single dash? Any number of dashes (e.g. more than 3) treated how?

  • Moderators

    either you remove/replace them with a QRegularExpression (-> QString::remove())
    Or you use a different approach:

    QStringList tokens;
    if( !data_[highLimit].isEmpty() ) tokens << data_[highLimit];
    if( !data_[pressure].isEmpty() ) tokens << data_[pressure];
    format = tokens.join("-");

  • @JonB
    leave one dash

  • @fender
    Then it looks like either of @raven-worx 's two approaches would achieve what you want.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.