Solved Remove spaces the from QString
-
Hi, I've been trying to remove the first space from a QString variable.
I'm making a all 10 fingers program (for keyboard) and I'm deleting characters one by one, but I'm having problem with spaces.
Here's my mainwindow.cpp#include "mainwindow.h" #include "ui_mainwindow.h" const QString article = "A quick red fox jumped over a lazy dog"; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->textEdit_Article->setText(article); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_lineEdit_TextInput_textChanged(const QString &arg1) { QString word = arg1; QString lastCharacter = arg1.back(); QString firstCharacter = arg1.front(); QString articleThing = article.front(); QString articleQ; if (lastCharacter == " ") { ui->lineEdit_TextInput->setText(""); word = ""; } if (firstCharacter == articleThing) { std::string articleRemove = article.toUtf8().constData(); if (firstCharacter == " ") { articleQ = articleQ.trimmed(); } else if (firstCharacter == "u\0000") { articleQ = articleQ.trimmed(); } else { articleRemove = articleRemove.erase(0,1); } articleQ = QString::fromStdString(articleRemove); ui->textEdit_Article->setText(articleQ); } }
Here's a snippet from mainwindow.cpp of the text characters deleting when it's typed:
void MainWindow::on_lineEdit_TextInput_textChanged(const QString &arg1) { QString word = arg1; QString lastCharacter = arg1.back(); QString firstCharacter = arg1.front(); QString articleThing = article.front(); QString articleQ; if (lastCharacter == " ") { ui->lineEdit_TextInput->setText(""); word = ""; } if (firstCharacter == articleThing) { std::string articleRemove = article.toUtf8().constData(); if (firstCharacter == " ") { articleQ = articleQ.trimmed(); } else if (firstCharacter == "u\0000") { articleQ = articleQ.trimmed(); } else { articleRemove = articleRemove.erase(0,1); } articleQ = QString::fromStdString(articleRemove); ui->textEdit_Article->setText(articleQ); } }
-
You can always try
QString::trimmed()
:
https://doc.qt.io/qt-5/qstring.html#trimmed -
Hi @ghutchis, I've used QString::trimmed() right here:
if (firstCharacter == articleThing) { std::string articleRemove = article.toUtf8().constData(); if (firstCharacter == " ") { articleQ = articleQ.trimmed(); } else if (firstCharacter == "u\0000") { articleQ = articleQ.trimmed(); } else { articleRemove = articleRemove.erase(0,1); } articleQ = QString::fromStdString(articleRemove); ui->textEdit_Article->setText(articleQ); }
Another of the problem I have, are that when I press space and qDebug it, it prints twice with:
" "
and
"u\0000"
EDIT: It prints the first character as the outputs shown up. I'm printing this:
QString firstCharacter = arg1.front(); -
str = str.simplified();
-
mystring = mystring.replace(" ","");
-
Hi, so @JoeCFD solution worked perfectly.
@Ketan__Patel__0011's solution did remove the spaces, but it removed EVERY space.
I had some things wrong in my code too, but anyway, thanks for the solutions :D -
@Sucharek said in Remove spaces the from QString:
I've been trying to remove the first space from a QString variable.
The quickest (short and reasonably performant) code to do just this is something like:
if (word.startsWith(' ')) word.remove(0, 1);
QString::simplified()
does quite a bit more than "remove the first space". Up to you. If you have given up on moving aQString
over to astd::string
, and then back again, to do your work, that is good.