Erasing Part Of A String

I'm trying to remove everything to the right of the first "/" including the "/". This is what I have so far. This is in Win7 in a C++ console program.

1
2
3
4
5
6
7
8
9
10
11
std::wstring wsURL;
	wsURL = bstr;
 
	size_t DSlashLoc = wsURL.find(L"//");
 
	if (DSlashLoc >= 0)
	{
		wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 2);
	}
 
	wprintf(L"  URL: %s\n\n", wsURL.c_str());


It is giving me this result.
www.cplusplus.com/forum/thread.cgi?w=new&i=852

from this...
http://www.cplusplus.com/forum/thread.cgi?w=new&i=852

and I need this
www.cplusplus.com

Any help is appreciated. Thank You.

Also tried this and no go....
1
2
3
4
5
6
7
std::wstring wsURL;
wsURL = bstr;
 
wsURL.TrimLeft("http://");
wsURL.TrimRight("/*");
	  
wprintf(L"  URL: %s\n\n", wsURL.c_str());


Last edited on
Try add this after line 9 of your code
1
2
3
         DSlashLoc = wsURL.find(L"/");
         if (DSlashLoc != wsURL.npos)
                 wsURL.erase(DSlashLoc);
Last edited on
Thank You. Works great. This is what I ended up with.

1
2
3
4
5
6
7
8
9
10
11
std::wstring wsURL;
	wsURL = bstr;
 	size_t DSlashLoc = wsURL.find(L"//www.");
 	if (DSlashLoc >= 0)
	{
		wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 6);
	}
		DSlashLoc = wsURL.find(L"/");
			if (DSlashLoc != wsURL.npos)
                 wsURL.erase(DSlashLoc);
 	wprintf(L"  URL: %s\n\n", wsURL.c_str());

Topic archived. No new replies allowed.