Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Remove spaces the from QString
Forum Updated to NodeBB v4.3 + New Features

Remove spaces the from QString

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sucharek
    wrote on last edited by
    #1

    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);
        }
    }
    
    Ketan__Patel__0011K JonBJ 2 Replies Last reply
    0
    • G Offline
      G Offline
      ghutchis
      wrote on last edited by
      #2

      You can always try QString::trimmed():
      https://doc.qt.io/qt-5/qstring.html#trimmed

      S 1 Reply Last reply
      1
      • G ghutchis

        You can always try QString::trimmed():
        https://doc.qt.io/qt-5/qstring.html#trimmed

        S Offline
        S Offline
        Sucharek
        wrote on last edited by Sucharek
        #3

        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();

        1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #4

          str = str.simplified();

          1 Reply Last reply
          3
          • S Sucharek

            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);
                }
            }
            
            Ketan__Patel__0011K Offline
            Ketan__Patel__0011K Offline
            Ketan__Patel__0011
            wrote on last edited by
            #5

            @Sucharek

            mystring = mystring.replace(" ","");
            
            1 Reply Last reply
            1
            • S Offline
              S Offline
              Sucharek
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • S Sucharek

                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);
                    }
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @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 a QString over to a std::string, and then back again, to do your work, that is good.

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved