URL出现修整问题 [英] Having Trim Issues With URL's

查看:106
本文介绍了URL出现修整问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在运行的代码.

This is the code I am running.

void PrintBrowserInfo(IWebBrowser2 *pBrowser) {
	BSTR bstr;
 	pBrowser->get_LocationURL(&bstr);
	wprintf(L"  1 URL: %s\n\n", bstr);
	myfile <<"\n  1 URL: << bstr;
	/////////////////////////////////
	std::wstring wsURL;
	wsURL = bstr;
 	size_t DSlashLoc = wsURL.find(L"//");
 	if (DSlashLoc != wsURL.npos)
	{
		wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 6);
	}
	DSlashLoc = wsURL.find(L"/");
	if (DSlashLoc != wsURL.npos)
		wsURL.erase(DSlashLoc);
 	wprintf(L"\n   2 URL: %s\n\n", wsURL.c_str());
	myfile <<"\n   2 URL: << wsURL.c_str();
	///////////////////////////////////
	SysFreeString(bstr);
}



探针就是这个……在第二组wprint上.
如果网址是http://www.codeproject.com
我得到
codeproject.com很棒.
但是,如果网址是http://stackoverflow.com
我得到
tackoverflow.com不太好.
我该如何解决?
打个招呼.



The probem is this...on the second set of wprint.
if a url is http://www.codeproject.com
I get
codeproject.com which is great.
however if a url is http://stackoverflow.com
I get
tackoverflow.com which is not so great.
How can I fix this?
Thnak you.

推荐答案

问题是您将DSlashLoc + 6作为结束偏移量.这将始终删除起始协议和冒号,//和接下来的4个字符.

The issue is that you have DSlashLoc + 6 as the end offset. This will always remove the starting protocol and colon, the // and the next 4 characters.

//First remove the http, https, ftp, ...
size_t DSlashLoc = wsURL.find(L"://");
if (DSlashLoc != wsURL.npos)
{
	wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 3);
}
//Now remove the www. if it exists, otherwise leave it alone
DSlashLoc = wsURL.find(L"www.");
if (DSlashLoc == 0)
{
	wsURL.erase(wsURL.begin(), wsURL.begin() + 4);
}


这篇关于URL出现修整问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆