我應該如何解析的QString,它包含系統變量我想要什麼?QString的解析系統變量
QString path = "%WINDIR%\\System32\\";
QString output = parse(path);
QDebug()<<output; \\ output is "C:\\Windows\\System32\\"
我應該如何解析的QString,它包含系統變量我想要什麼?QString的解析系統變量
QString path = "%WINDIR%\\System32\\";
QString output = parse(path);
QDebug()<<output; \\ output is "C:\\Windows\\System32\\"
我想你想是這樣的:
// Untested
QString parse(QString str)
{
int pos = 0;
QRegExp rx("%([^%]+)%"); // Match env var between two '%'
rx.setMinimal(true);
while((pos = rx.indexIn(str, pos)) != -1)
{
// Replace env var
QString capture = rx.cap(1);
QString replacement = getenv(capture.toAscii());
str.replace("%" + capture + "%", replacement);
// Skip env var + two '%'
pos += rx.matchedLength() + 2;
}
return str;
}
QString path = parse("%WINDIR%\\System32");
http://qt-project.org/doc/qt-5.0/qtcore/qprocessenvironment.html#systemEnvironment –
權,如馬立克r表示你可以用'QProcessEnvironment :: systemEnvironment()代替'GETENV(捕獲)'值(捕獲)'保持100%Qt。 – Silex
我想,這是你在尋找什麼。請試試這個
QString windir = getenv ("WINDIR"); // Expanded
if (windir.isEmpty()) {
fprintf(stderr, "Generator requires WINDIRto be set\n");
}
windir += "\\System32";
qDebug()<<windir;
你從輸出想要什麼? – dare
您可以使用分裂().. –
是啊,這個問題很含糊:□請註明它到底是什麼,你想達到:),你是什麼「分析」的意思是,你要提取每個路徑元素? –