我有字符串,我需要在每\n
之后将其拆分为新行。这在以下代码中成功完成:
#include <iostream>
#include <QtCore>
int main(int argc, char ** argv)
{
QString myString("diskinfo: Node,Description,FreeSpace,Name,Size \nASHUTOSH-PC,Local Fixed Disk,420935663616,C:,499875049472 \nASHUTOSH-PC,CD-ROM Disc,,D:, \nASHUTOSH-PC,Local Fixed Disk,324989792256,E:,487687450624 \nASHUTOSH-PC,CD-ROM Disc,0,F:,553459712");
QStringList myStringList = myString.split("\n");
for(int index =0;index < myStringList.length();index++)
{
std::cout<<myStringList.at(index).toStdString()<<std::endl;
}
}
上述代码的输出是:
Node,Description,FreeSpace,Name,Size
OSH-PC,Local Fixed Disk,420942745600,C:,499875049472
OSH-PC,CD-ROM Disc,,D:,
OSH-PC,Local Fixed Disk,324861591552,E:,487687450624
OSH-PC,CD-ROM Disc,0,F:,553459712
现在接下来我要做的是:
获取以下输出如何完成:
Node: OSH-PC Description:Local Fixed Disk, FreeSpace:420942745600, Name:C:,size:499875049472
Node: OSH-PC Description:CD-ROM, FreeSpace:, Name:D:,size:,
在Qt c ++中这怎么可能?
答案 0 :(得分:1)
好吧,你只需要用&#34;,&#34;分割QStringList的内部字符串(因为它们是QStrings)。分隔符,然后使用基本串联将分割的数据与分割的描述(QStringList的第一个元素)连接在一起。这是我的建议:
QStringList myStringList = myString.split("\n");
QStringList descriptions = myStringList[0].split(",");
descriptions[0] = descriptions[0].remove(0, descriptions[0].indexOf(" ") + 1);
for(int index = 1;index < myStringList.length();index++)
{
QStringList data = myStringList[index].split(",");
QStringList out;
for(int ind_2 = 0; ind_2 < data.length(); ind_2++)
out.push_back(descriptions[ind_2] + ": " +data[ind_2]);
QString out_str = out.join(", ");
std::cout << out_str.toStdString() << std::endl;
}
JFF,c ++ 11-heavy version:)
#include <iostream>
#include <QtCore>
#include <algorithm>
#include <vector>
int main(int argc, char ** argv)
{
QString myString("diskinfo: Node,Description,FreeSpace,Name,Size \nASHUTOSH-PC,Local Fixed Disk,420935663616,C:,499875049472 \nASHUTOSH-PC,CD-ROM Disc,,D:, \nASHUTOSH-PC,Local Fixed Disk,324989792256,E:,487687450624 \nASHUTOSH-PC,CD-ROM Disc,0,F:,553459712");
QStringList myStringList = myString.split("\n");
QStringList descriptions = myStringList[0].split(",");
descriptions[0] = descriptions[0].remove(0, descriptions[0].indexOf(" ") + 1);
std::vector<QStringList> data;
std::transform(myStringList.begin() + 1, myStringList.end(), std::back_inserter(data), [](QString& data_string) {
return data_string.split(",");
});
QStringList out_data;
std::transform(data.begin(), data.end(), std::back_inserter(out_data), [&descriptions](QStringList& list) {
QStringList with_desc;
for(size_t i = 0; i < list.size(); i++)
with_desc.push_back(descriptions[i] + ": " + list[i]);
return with_desc.join(",");
});
std::cout << out_data.join("\n").toStdString() << std::endl;
}
答案 1 :(得分:0)
这看起来像商业逻辑,如果您熟悉Multitier Architecture,您就会意识到将其与表示层分开的重要性。
在这种情况下,这是一件非常好的事情,因为您会发现c++11使用vector<vector<string>>
将sregex_token_iterator
分成const regex re{ "((?:[^\\\\,]|\\\\.)+)(?:,|$)" };
vector<vector<string>> descriptions;
for (auto it = cbegin(myStringList); it != cend(myStringList); ++it) descriptions.emplace_back(vector<string>{ cregex_token_iterator(it->cbegin(), it->cend(), re, 1), cregex_token_iterator() });
for (auto i = 1; i < size(descriptions); ++i) {
for (auto j = 0; j < size(descriptions.front()) - 1; ++j) {
cout << descriptions.front()[j] << ": " << descriptions[i][j] << ",\t";
}
cout << descriptions.front().back() << ": " << descriptions[i].back() << endl;
}
非常简单:
if (glob(...))
我在my comment中提到您输入的字符串与输出字符串不匹配。对于我的代码,我要求在每个分隔符之间至少有一个空白字符(&#39;,&#39;)。