当前位置 博文首页 > L_add的博客:C++ --取出网址中域名和协议名

    L_add的博客:C++ --取出网址中域名和协议名

    作者:[db:作者] 时间:2021-08-15 22:18

    取出网址中域名和协议名

    tring GetDomain(const string& url)//域名
    {
    	size_t pos = url.find("://");
    	if (pos != string::npos)
    	{
    		size_t sta = pos + 3;
    		size_t end = url.find("/", sta);
    		if (end != string::npos)
    		{
    			return url.substr(sta, end - sta);
    		}
    	}
    	else
    	{
    		return string();//匿名对象
    	}
    }
    string GetProtocol(const string& url)//协议名
    {
    	size_t pos = url.find("://");
    	if (pos != string::npos)
    	{
    		return url.substr(0, pos - 0);
    	}
    	else
    	{
    		return string();//匿名对象
    	}
    
    }
    int main()
    {
    	//要求分别取出域名和协议名
    	string url1 = "http://www.cplusplus.com/reference/string/";
    	string url2 = "https://www.cppreference.com/";
    	cout << GetDomain(url1) << endl;
    	cout << GetProtocol(url1) << endl;
    	cout << GetDomain(url2) << endl;
    	cout << GetProtocol(url2) << endl;
    	return 0;
    }
    
    cs
    下一篇:没有了