Substring c++

How can I take a string like this: 1: hello, wow
Substring wow and 1?
I assume you are talking about the substr() function? Well, here it is with its parameters:
 
substr(start_point, length);


Those arent exact parameters, but just in pseudocode. Anyway, in this instance, it would be:
for wow:
1
2
3
4
    string hi("1: hello, wow");
    string wow = hi.substr(10, 3);
    string one = hi.substr(0, 1);
    cout<<wow<<"\n";


for 1:
1
2
    string one = hi.substr(0, 1);
    cout<<one;


It is a part of the string library, so don't forget to include it. And, like arrays, the string's first character is a 0, for instance, in: "I went to the store", 0 would be "I". The first parameter (as labeled start_point) is which character you want to start at. The second (length) is how many characters you want to copy from start_point.
So, in "I went to the store", if you wanted to copy only the word "store", you would make it a string, and then use strcpy(), and for start_point you use 14, and for length you use 5, because "store" is 5 characters long.

I hoped this helped! Good luck with whatever you are doing.
Last edited on
Topic archived. No new replies allowed.