引用符で分割できます(引用符だけでなく、たとえば「\」などの任意の記号)記号をqtの\
前に置くと、例:記号でstring.split("\"");
分割string
され'"'
ます。
ファイルを分割するための簡単なコンソール アプリを次に示します (これまでのところ、最も簡単な解決策は "," 記号で分割することです)。
// opening file split.csv, in this case in the project folder
QFile file("split.csv");
file.open(QIODevice::ReadOnly);
// flushing out all of it's contents to stdout, just for testing
std::cout<<QString(file.readAll()).toStdString()<<std::endl;
// reseting file to read again
file.reset();
// reading all file to QByteArray, passing it to QString consructor,
// splitting that string by "," string and putting it to QStringList list
// where every element of a list is value from cell in csv file
QStringList list=QString(file.readAll()).split("\",\"",QString::SkipEmptyParts);
// adding back quotes, that was taken away by split
for (int i=0; i<list.size();i++){
if (i!=0) list[i].prepend("\"");
if (i!=(list.size()-1)) list[i].append("\"");
}//*/
// flushing results to stdout
foreach (QString i,list) std::cout<<i.toStdString()<<std::endl; // not using QDebug, becouse it will add more quotes to output, which is already confusing enough
split.csv
出力は"1","hello, ""world""","and then this"
次のとおりです。
"1"
"hello, ""world"""
"and then this"