Or use a QStringlist and use replaceInStrings(...)
Or use a QStringlist and use replaceInStrings(...)
NicholasSmith (6th August 2010)
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!
I see no problem with your syntax. so make a debug version an look at the values:
What does it returns?Qt 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(",", " "); }To copy to clipboard, switch view to plain text mode
NicholasSmith (6th August 2010)
at() and value() are const
You can't directly change a list like this, try this:
Qt Code:
#include <QtCore/QCoreApplication> #include <QStringList> #include <QDebug> int main(int argc, char *argv[]) { qDebug() << "Test replacing comma in a string list"; qDebug() << ""; QStringList list; 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(); }To copy to clipboard, switch view to plain text mode
It gives this output:
Qt 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"To copy to clipboard, switch view to plain text mode
NicholasSmith (6th August 2010)
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...EDIT: Without a temporary QString you have to use the [] operator.const T & at ( int i ) const
T value ( int i ) const
NicholasSmith (6th August 2010)
Thanks chaps! I decided to just go down the temporary string route, and it works perfectly now.
Ah hah, it is actually spotting it and triggering, it just doesn't seem to be replacing now.
Bookmarks