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. QStringList question
Qt 6.11 is out! See what's new in the release blog

QStringList question

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.3k Views 1 Watching
  • 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.
  • RIVOPICOR Offline
    RIVOPICOR Offline
    RIVOPICO
    wrote on last edited by RIVOPICO
    #1

    Hello i will have the follow string:

    QString Line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,ñ,o,p,q,r,s,t,u,v,w,y,x,z";
    

    And now i want to get this qstring using qstringlist:

    \"a\",\"b\",.........\"z\"
    

    So i do this:

    #include <QStringList>
    #include <QString>
    QString Line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,ñ,o,p,q,r,s,t,u,v,w,y,x,z";
    List = Line.split(",");
    QString After = List.join(",\"");
    

    So i got:

    "a\",b\",c\",d\",e\",f\",g\",h\",i\",j\",k\",l\",m\",n\",±\",o\",p\",q\",r\",s\"
    ,t\",u\",v\",w\",y\",x\",z""
    

    But i want to include like i said first /" then my letter again /" and semicolon. and at first one double quote " and the end other double quote". It's possible to make this with qstringlist???.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! Do you mean like this?

          QString line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,ñ,o,p,q,r,s,t,u,v,w,y,x,z";
          QStringList lst = line.split(',');
          for (auto &i: lst)
              i = QString("\"%1\"").arg(i);
          line = lst.join(',');
      
      VRoninV 1 Reply Last reply
      1
      • ? A Former User

        Hi! Do you mean like this?

            QString line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,ñ,o,p,q,r,s,t,u,v,w,y,x,z";
            QStringList lst = line.split(',');
            for (auto &i: lst)
                i = QString("\"%1\"").arg(i);
            line = lst.join(',');
        
        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        @Wieland
        from http://doc.qt.io/qt-5/containers.html#the-foreach-keyword

        the range-based for that is part of C++ 11 and newer. However, keep in mind that the range-based for might force a Qt container to detach

        Since the statement above is a bit generic I tend not use range based for for any Qt container

        // #include <QStringBuilder>
        QString line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,ñ,o,p,q,r,s,t,u,v,w,y,x,z";
            QStringList lst = line.split(',');
        const int listSize= lst.size();
        const QChar quoteChar('\"');
        for(int i=0;i<listSize;++i)
        lst[i] =  quoteChar % lst.at(i) % quoteChar;
        

        on the other hand, if you don't need the stringlist but just need the QString After you can use:

        QString Line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,ñ,o,p,q,r,s,t,u,v,w,y,x,z";
        QString After = '\"' + Line.replace(",","\",\"")+ '\"';
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        ? 1 Reply Last reply
        2
        • VRoninV VRonin

          @Wieland
          from http://doc.qt.io/qt-5/containers.html#the-foreach-keyword

          the range-based for that is part of C++ 11 and newer. However, keep in mind that the range-based for might force a Qt container to detach

          Since the statement above is a bit generic I tend not use range based for for any Qt container

          // #include <QStringBuilder>
          QString line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,ñ,o,p,q,r,s,t,u,v,w,y,x,z";
              QStringList lst = line.split(',');
          const int listSize= lst.size();
          const QChar quoteChar('\"');
          for(int i=0;i<listSize;++i)
          lst[i] =  quoteChar % lst.at(i) % quoteChar;
          

          on the other hand, if you don't need the stringlist but just need the QString After you can use:

          QString Line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,ñ,o,p,q,r,s,t,u,v,w,y,x,z";
          QString After = '\"' + Line.replace(",","\",\"")+ '\"';
          
          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @VRonin said

          However, keep in mind (...)

          Thank you. Good to know, and another reason to avoid Qt's container classes :)

          VRoninV 1 Reply Last reply
          1
          • ? A Former User

            @VRonin said

            However, keep in mind (...)

            Thank you. Good to know, and another reason to avoid Qt's container classes :)

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @Wieland said in QStringList question:

            another reason to avoid Qt's container classes

            A bit drastic for a solution...

            I tend not use range based for for any Qt container

            Sorry, I was too generic myself here, I mean: I tend not use range-based-for for any non const (or non const-able with something like std::as_const) Qt container

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            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