【发布时间】:2015-11-02 13:40:23
【问题描述】:
所以在我程序的另一部分,我从浏览器中读出了各种 url。
假设我有http://www.example.com 以及http://example.com 和https://example.com。对于浏览器,这三个 url 是不同的。
对我来说,只有“基本”域 (example.com) 很重要。
我现在试图从域中删除www,但是,不能成功。我想使用提供的 QUrl 库来执行此操作,而不是检查字符串是否包含 www. 并在之后将其删除。
如您所见,这里更像是一个设计决策;)
这是我当前的应用程序。
main.cpp
#include <QApplication>
#include <QDebug>
#include <QUrl>
#include <QList>
int main(int argc, char *argv[])
{
QList<QUrl> urlList;
urlList << QUrl("http://example.com/qwe/whoami/123#123141");
urlList << QUrl("chrome://newtab/");
urlList << QUrl("favorites://");
urlList << QUrl("");
urlList << QUrl("https://www.google.de/");
urlList << QUrl("https://google.de/");
urlList << QUrl("https://www.youtube.com/watch?v=XTPGpBBqwe");
urlList << QUrl("https://youtube.com/watch?v=189273ijadzqiuwejk");
urlList << QUrl("http://raspberrypi.stackexchange.com/questions/10371/whoisthisyo");
urlList << QUrl("https://stackoverflow.com/questions/33478464/alfresco-custom");
urlList << QUrl("http://localhost:3000");
urlList << QUrl("localhost:3000");
for (int i = 0; i < urlList.count(); i++) {
qDebug() << "[" << i+1 << "] " << urlList[i].host();
}
return 0;
}
感谢您的帮助!
【问题讨论】:
-
别打扰,只需使用
QString.replace()和www作为旧值,""作为新值 -
这是否意味着没有优雅的方式来实现我所描述的目标? ;p