How to extract characters from QString...?
-
I have this simple code to extract the characters and concatenate them to another string.
QString extract; QString stringA = "2024ABCDEF"; extract = "01-01-"; extract += stringA.left(4);
And under QT Creator (width QT 5.15.2) I have this message "Use leftRef() instead [clazy-qstring-ref]"
But I read that "leftRef" is deprecated as of QT 6.xSo I should continue to use left() and not leftRef() and ignore the message...?
Or use QStringView...? -
I have this simple code to extract the characters and concatenate them to another string.
QString extract; QString stringA = "2024ABCDEF"; extract = "01-01-"; extract += stringA.left(4);
And under QT Creator (width QT 5.15.2) I have this message "Use leftRef() instead [clazy-qstring-ref]"
But I read that "leftRef" is deprecated as of QT 6.xSo I should continue to use left() and not leftRef() and ignore the message...?
Or use QStringView...?@Juan-Dev said in How to extract characters from QString...?:
But I read that "leftRef" is deprecated as of QT 6.x
The whole
QStringRef
API/module will be removed in Qt7.Or use QStringView...?
Yes, see what @SGaist wrote here
(very last comment of the topic. He also linked to the to-be-released Qt6.9 documentation in Qt's dev branch) -
@Juan-Dev said in How to extract characters from QString...?:
But I read that "leftRef" is deprecated as of QT 6.x
The whole
QStringRef
API/module will be removed in Qt7.Or use QStringView...?
Yes, see what @SGaist wrote here
(very last comment of the topic. He also linked to the to-be-released Qt6.9 documentation in Qt's dev branch)@Pl45m4
First of all, thank you for this response.
Then I looked at QStringView but if I use my previous code how can I extract characters and concatenate them to a QString ?
I can simply display extraction like that :
qDebug() << QStringView{ stringA }.left(4);
But how to transform this into QString...? -
I have this simple code to extract the characters and concatenate them to another string.
QString extract; QString stringA = "2024ABCDEF"; extract = "01-01-"; extract += stringA.left(4);
And under QT Creator (width QT 5.15.2) I have this message "Use leftRef() instead [clazy-qstring-ref]"
But I read that "leftRef" is deprecated as of QT 6.xSo I should continue to use left() and not leftRef() and ignore the message...?
Or use QStringView...?@Juan-Dev Try first() instead.
-
@Pl45m4
First of all, thank you for this response.
Then I looked at QStringView but if I use my previous code how can I extract characters and concatenate them to a QString ?
I can simply display extraction like that :
qDebug() << QStringView{ stringA }.left(4);
But how to transform this into QString...?@Juan-Dev QString has an overload for operator+ that allows for a QStringView object.
So you don't have to change much at all:
int main() { QString stringA = "2024ABCDEF"; QStringView stringViewA(stringA); QString extract = "01-01-"; extract += stringViewA.left(4); qDebug() << extract; return 0; }
-
@Pl45m4
First of all, thank you for this response.
Then I looked at QStringView but if I use my previous code how can I extract characters and concatenate them to a QString ?
I can simply display extraction like that :
qDebug() << QStringView{ stringA }.left(4);
But how to transform this into QString...?@Juan-Dev said in How to extract characters from QString...?:
I can simply display extraction like that :
qDebug() << QStringView{ stringA }.left(4);
But how to transform this into QString...?Isn't the whole point of using
QStringView
to not have to create additional sub-string variables or refs in the process?QString stringA = "ABCDEFGH"; QStringView abcd = QStringView{ stringA }.left(4); // some further string manipulation // ... // make string again once you are done QString str = abcd.toString(); qDebug() << str;
-
@Juan-Dev said in How to extract characters from QString...?:
I can simply display extraction like that :
qDebug() << QStringView{ stringA }.left(4);
But how to transform this into QString...?Isn't the whole point of using
QStringView
to not have to create additional sub-string variables or refs in the process?QString stringA = "ABCDEFGH"; QStringView abcd = QStringView{ stringA }.left(4); // some further string manipulation // ... // make string again once you are done QString str = abcd.toString(); qDebug() << str;
-
@Juan-Dev QString has an overload for operator+ that allows for a QStringView object.
So you don't have to change much at all:
int main() { QString stringA = "2024ABCDEF"; QStringView stringViewA(stringA); QString extract = "01-01-"; extract += stringViewA.left(4); qDebug() << extract; return 0; }
-
@Pl45m4
Thank you for your answer. I finally opted for the method provided by @J-Hilk but it is true that it is not necessarily useful to create additional variables with QStringView -
@Juan-Dev
Is it easier to use std::string to handle it and convert it to QString when needed? Then Qt version issue will not matter any more.
https://cplusplus.com/reference/string/string/substr/ -
J Juan Dev has marked this topic as solved on
-
@Juan-Dev
Is it easier to use std::string to handle it and convert it to QString when needed? Then Qt version issue will not matter any more.
https://cplusplus.com/reference/string/string/substr/@JoeCFD said in How to extract characters from QString...?:
Is it easier to use std::string to handle it and convert it to QString when needed?
I had bad experiences with mixing QString and std::string. There have been times (on Windows with MSVC) where some references to std::string could not be found when using QString::toStdString(). Now, we have set the regular C++ locale to be UTF-8 (which means all std::strings are UTF-8). This also means that we need to use QString::fromUtf8(stdstring.c_str()) and qtstring.toUtf8().data() to convert between QString and std::string. (Under some circumstances the conversion from QString to std::string has some weird behaviour because of calling data() on a temporary object. It would be better to store the result of toUtf8() in a QByteArray before.)
So, bad experiences with QString::toStdString/fromStdString() together with UTF-8 std::strings tell me it is not easier to go back and forth between std::string and QString. (Also, QString::mid() might have worse performance than QString::midRef(), but certainly better performance than converting back and forth.)