如何从Qt中的字符串中获取特定字段的值
How to get value of a particular field from a string in Qt
如何在 Qt 中获取特定查询字段的值。
考虑以下字符串
"plugin://plugin.video.youtube/?action=play_video&videoid=4fVCKy69zUY"
我想要 videoid 的值,即字符串形式的 4fVCKy69zUY 我该怎么做。
我觉得Qstring.mid(int index , int len)
可以帮到你
因为它是一个 URL 你试图解构,所以使用专用的 QUrl
和 QUrlQuery
类:
QUrl url("plugin://plugin.video.youtube/?action=play_video&videoid=4fVCKy69zUY");
QUrlQuery q(url);
QString videoid = q.queryItemValue("videoid");
QUrlQuery
有一个接受 QString
的构造函数,所以
QUrlQuery q("plugin://plugin.video.youtube/?action=play_video&videoid=4fVCKy69zUY");
如果您真的只想从 URL 中提取视频 ID, 也可以使用,但我怀疑 QUrl
可能在其他方面对您有用。
或者,使用正则表达式:
QString url = "plugin://plugin.video.youtube/?action=play_video&videoid=4fVCKy69zUY";
// Regex to match the videoid. The capture ID is only the ID itself (between
// the parentheses): everything after "videoid=" that is not an ampersand,
// which is where the videoid ends and the next parameter begins.
QRegExp re("videoid=([^&]*)");
// apply it
re.indexIn(url);
// extract the capture
QString videoid = re.cap(1);
您可以使用 QString::mid
检索 videoid=
之后的文本:
QString str = "plugin://plugin.video.youtube/?action=play_video&videoid=4fVCKy69zUY";
QString strToFind = "videoid=";
QString value = str.mid(str.indexOf(strToFind)+strToFind.length());
如何在 Qt 中获取特定查询字段的值。 考虑以下字符串
"plugin://plugin.video.youtube/?action=play_video&videoid=4fVCKy69zUY"
我想要 videoid 的值,即字符串形式的 4fVCKy69zUY 我该怎么做。
我觉得Qstring.mid(int index , int len)
可以帮到你
因为它是一个 URL 你试图解构,所以使用专用的 QUrl
和 QUrlQuery
类:
QUrl url("plugin://plugin.video.youtube/?action=play_video&videoid=4fVCKy69zUY");
QUrlQuery q(url);
QString videoid = q.queryItemValue("videoid");
QUrlQuery
有一个接受 QString
的构造函数,所以
QUrlQuery q("plugin://plugin.video.youtube/?action=play_video&videoid=4fVCKy69zUY");
如果您真的只想从 URL 中提取视频 ID, 也可以使用,但我怀疑 QUrl
可能在其他方面对您有用。
或者,使用正则表达式:
QString url = "plugin://plugin.video.youtube/?action=play_video&videoid=4fVCKy69zUY";
// Regex to match the videoid. The capture ID is only the ID itself (between
// the parentheses): everything after "videoid=" that is not an ampersand,
// which is where the videoid ends and the next parameter begins.
QRegExp re("videoid=([^&]*)");
// apply it
re.indexIn(url);
// extract the capture
QString videoid = re.cap(1);
您可以使用 QString::mid
检索 videoid=
之后的文本:
QString str = "plugin://plugin.video.youtube/?action=play_video&videoid=4fVCKy69zUY";
QString strToFind = "videoid=";
QString value = str.mid(str.indexOf(strToFind)+strToFind.length());