substr
Aug 24, 2010 at 8:03pm UTC
Hi, when calling this function:
1 2 3 4 5
string getChannel(string res)
{
string s = res.substr(res.find("#" ), res.length());
return s.substr(1, s.find(" " ));
}
the program terminates without giving any reason.
Last edited on Aug 24, 2010 at 8:11pm UTC
Aug 25, 2010 at 5:24am UTC
That's because you're trying to extract more characters than there are if # is not the first character of res.
The function will also not work correctly if the passed string does not contain a space or '#'.
Aug 26, 2010 at 12:43am UTC
So i should do like:
string s = res.substr(res.find("#"), res.length()-res.find("#")) ?
Aug 26, 2010 at 6:08pm UTC
That would work, yes. Assuming that there is a # in the string.
Aug 27, 2010 at 3:29am UTC
I'm pretty sure if the 2nd arg of substr is longer then the string, you just get the entire string.
The function will also not work correctly if the passed string does not contain a space or '#'.
That is the issue I believe.
Topic archived. No new replies allowed.