how to trim or normalize a QString?
-
how to trim or normalize a QString? mainly remove the space and comma.
eg. " , a,b ,c, d,," to "a,b,c,d" -
@opengpu the easiest way is:
QString noSpace = myString.split(' ', QString::SkipEmptyParts).join("");
@KroMignon said in how to trim or normalize a QString?:
the easiest way is:
I disagree, the easiest way is to use
https://doc.qt.io/qt-5/qstring.html#trimmed
or
https://doc.qt.io/qt-5/qstring.html#simplified;-)
edit:
forgot the comma requirement:
https://doc.qt.io/qt-5/qstring.html#replace-3 -
u mean 1st replace all "," to " ", and then simplified?
and then split by " ", i will get "a" "b" "c" "d" in the QStringlist, right?@opengpu
well yes, and no.If your goal is to have all normal chars in a QStringList, than using @KroMignon split with the extra argument is probably faster and easier:
auto list = myString.split(',' , QString::SkipEmptyParts);
-
how to trim or normalize a QString? mainly remove the space and comma.
eg. " , a,b ,c, d,," to "a,b,c,d"@opengpu said in how to trim or normalize a QString?:
mainly remove the space and comma.
Oups, I think a read/reply too quickly your question: perhaps is should be:
auto listParts = myString.split(QRegExp(" |,"), QString::SkipEmptyParts);
This will remove all spaces and coma and generate a list with the elements.
perhaps a better implementation could beauto listParts = myString.remove(' ').split(',');
So you could get also empty elements.
-
@opengpu said in how to trim or normalize a QString?:
mainly remove the space and comma.
Oups, I think a read/reply too quickly your question: perhaps is should be:
auto listParts = myString.split(QRegExp(" |,"), QString::SkipEmptyParts);
This will remove all spaces and coma and generate a list with the elements.
perhaps a better implementation could beauto listParts = myString.remove(' ').split(',');
So you could get also empty elements.
@KroMignon said in how to trim or normalize a QString?:
auto listParts = myString.split(QRegExp(" |,"), QString::SkipEmptyParts);
QRegExp
is deprecated; I suggestQRegularExpression
instead.