Solved QStringList question
-
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???.
-
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(',');
-
@Wieland
from http://doc.qt.io/qt-5/containers.html#the-foreach-keywordthe 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(",","\",\"")+ '\"';
-
@VRonin said
However, keep in mind (...)
Thank you. Good to know, and another reason to avoid Qt's container classes :)
-
@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