[C++] Get all characters after a certain place in a string?

Soldato
Joined
6 Jan 2005
Posts
3,633
Location
Cambridge
Hi, I've been looking on google for quite a while and can't really find out how to do this.

Basically let's say I have a string that says
Code:
Download: http://www.whatever.com/update.exe

I can find out the "Download: " part with
Code:
if(string(sReceived).find("Download: ") != string::npos)
            {
                cout << "Download Found";
            }

How can I get the "http://www.whatever.com/update.exe" part so I know what to download?

Thanks,
 
I would do it like this:

Code:
#include <iostream>
#include <vector>
#include <sstream>
#include <string>

using namespace std;

int main()
{
	string URL("Download: http://www.whatever.com/update.exe");
	istringstream ss(URL);
	vector<string> subStrings;
	
        // Extract sub strings using white space as delimiter
	do
	{
		string sub;
		ss >> sub;
		subStrings.push_back(sub);
	} while (ss);

	// Know our URL is at index 2 in the vector
	cout<<"Yo, URL is: "<<subStrings.at(1)<<endl;

	return 0;
}

Output: Yo, URL is: http://www.whatever.com/update.exe

There are more efficient/elegant methods using tokenizing, if you're bothered.
 
and here's how I would do it...

Code:
#include <stdio.h>
#include <string.h>

int main( void )
{
	char szCommand[] = "Download: http://www.whatever.com/update.exe";
	char *link = strrchr( szCommand, ' ' );
	printf( "%s\n", link + 1 );

	return 0;
}
 
and here's how I would do it...

Code:
#include <stdio.h>
#include <string.h>

int main( void )
{
	char szCommand[] = "Download: http://www.whatever.com/update.exe";
	char *link = strrchr( szCommand, ' ' );
	printf( "%s\n", link + 1 );

	return 0;
}

That's nice, but it's not C++ and a char array is not comparable to an std::string.
 
Back
Top Bottom