-3

I am trying to substring some expressions into individual tokens such as !, &, | (), etc. What I am having trouble with is the fact that when I try to make a sub-string of "!(S&B|H)&!(S&J|R)&!(P)" with the cout line below, I get: "(S&J|R)&!(P)", when I thought it should be: "(S&J|R)". It either is beyond what I have seen or just so simple that I just am not getting it. Any help would help a lot. Thanks.

#include <iostream>
#include <string>

using namespace std;

int main(int argc, const char * argv[]) {

string name = "!(S&B|H)&!(S&J|R)&!(P)";

cout<<name.substr(10,16)<<endl;


return 0;

}//Main
  • 3
    You seem to have forgotten to mention what happens when you try it and what the problem is. Anyway, note that the 2nd parameter of `substr` is the length, not the end of the range. – underscore_d Jul 16 '15 at 22:04
  • @underscore_d: but substr() should stop reading at the end of the string – Thomas Weller Jul 16 '15 at 22:06
  • I'm sure. But the OP might not _want_ it to go all the way to the end, so it's worth pointing out in case there was a mistaken expectation. Anyway, without any indication of what the problem is, vs. the expected result, we can't do a lot here. – underscore_d Jul 16 '15 at 22:08
  • 1
    @underscore_d You were correct in stating that the second parameter was the length not the range. That's exactly what I needed to know. Managed to skip that. Thank you! – brianaguirre Jul 16 '15 at 22:13
  • Glad to hear it, and you're welcome! – underscore_d Jul 16 '15 at 22:19

1 Answers1

2

I did not understand your question well but if you want to get

(S&J|R)

You should do:

name.substr(10,7)

The second parameter is the length.

brettwhiteman
  • 4,210
  • 2
  • 29
  • 38
syscherry
  • 41
  • 6
  • 1
    This is what I needed. I read the library at 3 AM this morning after trying to figure it out but did not look back. Thank you so much! – brianaguirre Jul 16 '15 at 22:12