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?

Recommended Answers

All 3 Replies

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.

great! Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.