Home > database >  QString::replace doesnt replace
QString::replace doesnt replace

Time:08-12

Problem

I am trying to convert a string to a C string. In doing so, I need to replace " with \". I use the following code to do this:

QString Converter::plain2C(const QString &in) {
    QString out;

    // Split input in each line
    QStringList list = in.split(QChar('\n'));
    for (int i = 0; list.length() > i; i  ) { // Go throught each line of the input
        QString line = list[i]; // Thats the line

        line.replace(QChar('\\'), QLatin1String("\\\\")); // replace \ with \\
        line.replace(QChar('"'), QLatin1String("\\\"")); // replace " with \"

        // For printf()
        if(escapePercent)
            line.replace(QChar('%'), QLatin1String("%%"));

        // If option "Split output into multiple lines" is active add a " to the output
        if (multiLine)
            out.append(QChar('"'));

        // append the line to the output
        out.append(line);

        // append a "\n" to the output because we are at the end of a line
        if (list.length() -1 > i)
            out.append(QLatin1String("\\n"));

        // If option "Split output into multiple lines" is active add a " and \n to the output
        if (multiLine) {
            out.append(QChar('"'));
            out.append(QChar('\n'));
        }
    }

    if (!multiLine) {
        out.prepend(QChar('"'));
        out.append(QChar('"'));
    }

    return out;
}

However, " is still there without a \ before.

Information

  • Qt Version 5.15.3
  • C 17

Edit

The application is used to enter a normal string copied from the Internet and get as a result a string that can be copied into a C/C program.

More code

void Converter::run()
{
    if (_from != From::NotSupportet &&
            _to != To::toInvalid) {
        QString out;

        // More code obove and below
        else if (_from == From::Plain) {
            switch (_to) {
            case To::toCString:
                out = plain2C(_in);
                break;
            
        // Emits the ready string which is applied direct to a QPlainTextEdit
        emit htmlReady(out);
    }
}

Edit 2

Added more comments to the code

CodePudding user response:

It works now. The problem was the line above:

line.replace(QChar('\'), QLatin1String("\\\\")); // replace \ with \\

The problem was that the comment ended with 2 \. That somehow disabled the next line or something like that.

Anyway, this is the working code:

QString Converter::plain2C(const QString &in) {
    QString out;

    // Split input in each line
    QStringList list = in.split(QChar('\n'));
    for (int i = 0; list.length() > i; i  ) { // Go throught each line of the input
        QString line = list[i]; // Thats the line

        line.replace(QChar('\\'), QLatin1String("\\\\")); // replace "\" with "\\"
        line.replace(QChar('"'), QLatin1String("\\\"")); // replace " with \"

        // For printf()
        if(escapePercent)
            line.replace(QChar('%'), QLatin1String("%%"));

        // If option "Split output into multiple lines" is active add a " to the output
        if (multiLine)
            out.append(QChar('"'));

        // append the line to the output
        out.append(line);

        // append a "\n" to the output because we are at the end of a line
        if (list.length() -1 > i)
            out.append(QLatin1String("\\n"));

        // If option "Split output into multiple lines" is active add a " and \n to the output
        if (multiLine) {
            out.append(QChar('"'));
            out.append(QChar('\n'));
        }
    }

    if (!multiLine) {
        out.prepend(QChar('"'));
        out.append(QChar('"'));
    }

    return out;
}
  • Related