Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved]How to remove the spaces before and after two words but not remove the space between the two words?



  • Hi everyone,
    I have difficulty in solving the above problem.
    I had encountered a problem in removing the space between the words.

    For example, when I put " <spaces> Hi everyone <spaces> ", I want only to get the string "Hi everyone".
    I came across this function, "setInputMask".
    But it still doesn't solve my problem.
    Can anyone help me?
    Maybe you can give some clue or explain on how exactly this function works?

    Btw, this is my code snippet:

    @
    ui->lineEdit->setInputMask("nnnnn");
    if (ui->lineEdit->text()== NULL)
    {
    QMessageBox::warning(this,tr("Warning"),
    tr("NULL"),
    QMessageBox::Ok) ;
    return;
    }
    else
    ui->textBrowser->setText(ui->lineEdit->text());
    @


  • Lifetime Qt Champion

    Hi,

    Are you thinking of something like QString::trimmed() ?



  • Yup, SGaist.
    Thanks for the suggestion.
    That's really helps me. :)



  • There is also QString::simplified(). That one does a bit more, in that it also removes things like double spaces between words (reduces them to a single space), as well as trimming.



  • ok, thanks Andre...


Log in to reply