QString contains fails to find comma
Hi guys, I reckon I'm missing something fairly simple but I'm baffled at what. I'm passing a function a QList<QString> array of a load of info pulled out of a database, then processing it down into a CSV file with some tweaks. All works well up until it hits anything with a comma in it, the issue now is my code doesn't seem to find it.
Code:
if(passedList.value(i).contains(","))
{
qDebug() << "replacing";
passedList.value(i).replace(",", " ");
}
That's basically what I've been using, but it never seems to be tripped, and I'm wondering if I'm using .contains wrong in that method.
Any help would be appreciated!
Re: QString contains fails to find comma
It does not reach the scope or is just not replaced. The later is because value returns a copy, so you need to use QList::at().
Re: QString contains fails to find comma
Or use a QStringlist and use replaceInStrings(...)
Re: QString contains fails to find comma
Thanks for the quick responses.
Lykurg: I tried with both .value(i) and .at(i) and neither trigger when the condition is met. Unless you mean in a manner different to passedList.at(i).contains.
tbscope: that's my other option, I wasn't aware of it when I first wrote my function and it'd involve a bit of refactoring to get it changed. I'll probably go down that route when I give up trying to get the .contains working. Intellectual challenge, or working out why I'm being a bit slow!
Re: QString contains fails to find comma
I see no problem with your syntax. so make a debug version an look at the values:
Code:
qDebug() << passedList;
qDebug() << i;
qDebug() << passedList.value(i);
qDebug() << passedList.value(i).contains(",");
if(passedList.value(i).contains(","))
{
qDebug() << "replacing";
passedList.value(i).replace(",", " ");
}
What does it returns?
Re: QString contains fails to find comma
at() and value() are const
You can't directly change a list like this, try this:
Code:
#include <QtCore/QCoreApplication>
#include <QStringList>
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug() << "Test replacing comma in a string list";
qDebug() << "";
list << "a, b, c" << "d, e, f";
for (int c = 0; c < list.count(); ++c) {
qDebug() << "Line" << c+1 << "=" << list.at(c);
}
qDebug() << "";
qDebug() << "And now without a comma:";
for (int c = 0; c < list.count(); ++c) {
line.replace(',', ' ');
list.replace(c, line);
qDebug() << "Line" << c+1 << "=" << list.at(c);
}
return a.exec();
}
It gives this output:
Code:
Test replacing comma in a string list
Line 1 = "a, b, c"
Line 2 = "d, e, f"
And now without a comma:
Line 1 = "a b c"
Line 2 = "d e f"
Re: QString contains fails to find comma
Ah hah, it is actually spotting it and triggering, it just doesn't seem to be replacing now.
Re: QString contains fails to find comma
Quote:
Originally Posted by
tbscope
at() and value() are const
Ah, I messed it up. Once again. at() is returning a const reference, but value returning a value (which is not const for the returning value, this it what I meant). I will never keep that in mind...
Quote:
const T & at ( int i ) const
T value ( int i ) const
EDIT: Without a temporary QString you have to use the [] operator.
Re: QString contains fails to find comma
Thanks chaps! I decided to just go down the temporary string route, and it works perfectly now.