简体   繁体   中英

Convert a multiline QString into a one line QString

I have something like this:

void ReadFileAndConvert ()
{
    QFile File (Directory + "/here/we/go");

    if(File.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream Stream (&File);
        QString Text;

        do
        {
            Text = Stream.readLine();
            Text = Text.simplified();
            // Here I want to convert the multiline QString Text into a oneline QString

// ...
}

The QString Text consists of a multiline Text that I need to convert into a online Text/QString. How can I achieve this? greetings

Put your text into a QStringList , and use QStringList::join() , eg

QStringList doc;
[...]
Text = Stream.readLine();
Text = Text.simplified();
doc << Text;
[...]
QString final = doc.join(" ");

You could use the readAll function of QTextStream in order to get a string containing all your text and then use the replace function of QString in order to remove new lines:

QString oneLineText = Stream.readAll().replace("\n"," ").simplified();

If you have a large file it is better to use the readLine function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM