Doubts about QString manipulation
-
I've written the following code-
Line 1 : QString myStr = "3.1415161718"; Line 2 : int sz = myStr.size(); //sz=12 Line 3: qDebug()<<"last digit ="<<myStr[sz-1]; // prints -> last digit = '8' Line 4: QString last_dig=myStr[sz-1] //this line gives errorLine 3works perfectly whereasLine 4gives an error. So, how can I store the required element of a string in another string?Edit:
I tried the above code with the following condition, it gives me the messageCondition satisfiedeven when the condition is not met.if (myStr[sz-1]<5) qDebug()<<"Condition satisfied";I tried to convert it to int and wrote the following code:
if (myStr[sz-1].toInt()<5) qDebug()<<"Condition satisfied";This gave me error.
-
@JKSH said in Doubts about QString manipulation:
@Swati777999 said in Doubts about QString manipulation:
Line 4: QString last_dig=myStr[sz-1] //this line gives errorWhat does the error message say?
It says
no viable conversion from 'QCharRef' to 'QString'.@Swati777999 QString's [] operator gives u QChar or QCharRef. You can't assign them straight to a QString. If you want to get the character as digit, you can do
int last_digit = myStr[zi-1].digitValue();Or if you really want them in string
QString last_digit(myStr[zi-1]); -
I've written the following code-
Line 1 : QString myStr = "3.1415161718"; Line 2 : int sz = myStr.size(); //sz=12 Line 3: qDebug()<<"last digit ="<<myStr[sz-1]; // prints -> last digit = '8' Line 4: QString last_dig=myStr[sz-1] //this line gives errorLine 3works perfectly whereasLine 4gives an error. So, how can I store the required element of a string in another string?Edit:
I tried the above code with the following condition, it gives me the messageCondition satisfiedeven when the condition is not met.if (myStr[sz-1]<5) qDebug()<<"Condition satisfied";I tried to convert it to int and wrote the following code:
if (myStr[sz-1].toInt()<5) qDebug()<<"Condition satisfied";This gave me error.
@Swati777999 said in Doubts about QString manipulation:
Line 4: QString last_dig=myStr[sz-1] //this line gives errorWhat does the error message say?
-
@Swati777999 said in Doubts about QString manipulation:
Line 4: QString last_dig=myStr[sz-1] //this line gives errorWhat does the error message say?
@JKSH said in Doubts about QString manipulation:
@Swati777999 said in Doubts about QString manipulation:
Line 4: QString last_dig=myStr[sz-1] //this line gives errorWhat does the error message say?
It says
no viable conversion from 'QCharRef' to 'QString'. -
@JKSH said in Doubts about QString manipulation:
@Swati777999 said in Doubts about QString manipulation:
Line 4: QString last_dig=myStr[sz-1] //this line gives errorWhat does the error message say?
It says
no viable conversion from 'QCharRef' to 'QString'.@Swati777999 said in Doubts about QString manipulation:
It says
no viable conversion from 'QCharRef' to 'QString'.https://doc.qt.io/qt-5/qstring.html#QString-8 -- QString constructors can take
QChar, but notQCharRef.You are using the version of
operator[]that returns a QCharRef: https://doc.qt.io/qt-5/qstring.html#operator-5b-5dSo, you can convert your QCharRef to a QChar first.
-
@JKSH said in Doubts about QString manipulation:
@Swati777999 said in Doubts about QString manipulation:
Line 4: QString last_dig=myStr[sz-1] //this line gives errorWhat does the error message say?
It says
no viable conversion from 'QCharRef' to 'QString'.@Swati777999 QString's [] operator gives u QChar or QCharRef. You can't assign them straight to a QString. If you want to get the character as digit, you can do
int last_digit = myStr[zi-1].digitValue();Or if you really want them in string
QString last_digit(myStr[zi-1]); -
@Swati777999 said in Doubts about QString manipulation:
It says
no viable conversion from 'QCharRef' to 'QString'.https://doc.qt.io/qt-5/qstring.html#QString-8 -- QString constructors can take
QChar, but notQCharRef.You are using the version of
operator[]that returns a QCharRef: https://doc.qt.io/qt-5/qstring.html#operator-5b-5dSo, you can convert your QCharRef to a QChar first.
@JKSH
This seems a lil complicated to me. Is not it going to be lengthy? -
@Swati777999 QString's [] operator gives u QChar or QCharRef. You can't assign them straight to a QString. If you want to get the character as digit, you can do
int last_digit = myStr[zi-1].digitValue();Or if you really want them in string
QString last_digit(myStr[zi-1]);@Sivan Thanks! This worked for me. :)