QString+char split
-
Hello All
I have string text for example "98745.95", "98745,95", "98745/95" and i want it to divide into two parts:
QString a1="98745.95" //or "98745/95" or "98745,95"
char separators[] ={' . ' , ' , ' , ' / '};
QString parts = a1.split(separators); // Here it show error;
int n1=parts[0].length(); // Here it must be length of "98745"
int n2=parts[1].length(); // Here it must be length of "95"
Question is how could i implement this with char variable?P.S.
This code works, but i want to try with code above
QString a1="98745.95";
QStringList strList = a1.split("."); // Here i show separator exactly
QString parts[2];
parts[0]=strList.at(0);
parts[1]=strList.at(1);
qDebug() << parts[0] << "-" << parts[1];Regards Jasur,
from Tashkent -
Hi @Jasur ,
please try the following:
#include <QCoreApplication> #include <QDebug> #include <QString> #include <QRegularExpression> int main(int argc, char *argv[]) { QString a1 = "98745.95"; //or "98745/95" or "98745,95" QStringList parts = a1.split(QRegularExpression("[\\.,/]")); qDebug() << parts; }
Also take care after split, if you access
parts[1]
orparts.at(1)
, you have to check thatparts.size() > 1
before or you get a crash if the string could not be split ;)