Hi,

I have a string that looks like this

\\x\\y\\z-Select * from x where y=z

I need to split this at the '-' into two strings. When I use substr as,

string s = "\\x\\y\\z-Select * from x where y=z";
size_t p = s.find("-");
if(p!=string::npos)
   string query = s.substr(p+1);

but how do i get the part before the position?

s.substr(0,p) The first argument is the starting position; the second is the length. If you omit the length, you get the longest possible substring with the given starting position, which is why s.substr(p+1) works.

Oky for more explanation.

In the C++ Annotations tutorial, it describes.

string::substr()
* without arguments , a copy of the string itself is returned.
* The first argument may be used to specify the offset of the first character to be returned.
* The second argument may be used to specify the number of characters that are to be
returned.

Note that the second argument 'number of characters' it's not the offset index.
PS: C++ Annotations is a free OpenSource package.So I copied with free.

This question has already been answered. Start a new discussion instead.