在Qt c ++中将字符串拆分为新行
Qstring是
Qstring str = "diskinfo: Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 418581491712, Name: C:, Size : 499875049472 Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: , Name: D:, Size : Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 324982935552, Name: E:, Size : 487687450624 Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: 0, Name: F:, Size : 553459712"
预期出局是
"diskinfo: Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 420842713088, Name: C:, Size : 499875049472
Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: , Name: D:, Size :
Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 324858568704, Name: E:, Size : 487687450624
Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: 0, Name: F:, Size : 553459712"
那就是它应该只在有空格而不是有逗号后跟空格时分开?即它应该从每一行的Node开始?任何人都可以帮助如何在Qt c ++中完成这项工作
现在以下是我的代码写的:
QString Utils::getDiskSpace( )
{
static QString diskSpaceCmd ( qgetenv("WINDIR")
+ "\\system32\\wbem\\wmic logicaldisk get name, freespace, size, description /format:csv");
QProcess proc;
QByteArray qba;
QString out_str;
QString newStr;
proc.start(diskSpaceCmd);
if(proc.waitForFinished(-1))
{
qba = proc.readAllStandardOutput();
qba = qba.trimmed();
qba =qba.replace('\r',' ');
QString myString =qba;
QStringList myStringList = myString.split("\n");
QStringList descriptions = myStringList[0].split(",");
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]);
out_str += out.join(", ");
}
out_str = out_str.replace("Node:", "\nNode:");
QStringList list;
if( !out_str.startsWith( '\n' ) )
out_str+=' ';
for( int i = 0; i < out_str.count( '\n' ); ++i ){
list << out_str.section( '\n', i, i, QString::SectionIncludeTrailingSep ).simplified();
}
foreach( QString out_str, list )
QLOG_DEBUG()<<"Out_Str" << out_str;
}
QString diskSpace(out_str);
return (diskSpace);
}
答案 0 :(得分:0)
可能听起来很明显但是,如何使用QString::split?
QStringList strList = str.split(QRegularExpression("\WNode"),QString::SkipEmptyParts);
然后,您可以使用prepend
在每个字符串的开头添加“节点”字样。
我就是这样做的。