Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString+char split
Forum Updated to NodeBB v4.3 + New Features

QString+char split

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 651 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jasur
    wrote on last edited by Jasur
    #1

    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

    aha_1980A 1 Reply Last reply
    0
    • J Jasur

      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

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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] or parts.at(1), you have to check that parts.size() > 1 before or you get a crash if the string could not be split ;)

      Qt has to stay free or it will die.

      J 1 Reply Last reply
      2
      • aha_1980A aha_1980

        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] or parts.at(1), you have to check that parts.size() > 1 before or you get a crash if the string could not be split ;)

        J Offline
        J Offline
        Jasur
        wrote on last edited by
        #3

        @aha_1980 Thank you very much it works. This is what i wanted.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved