Why is string.find_first_of behaving this way?

Viewed 51

I am trying to make a (assembly) parser which uses a string as a guide for how to cut the text to get the tokens I want.

string s = "$t4,";
string guide = "$!,$!,$!";
int i = 1;
string test =s.substr(0, s.find_first_of(" ,.\t"+to_string(guide[i+1]) ));
cout <<  test << "\n";

if s = "$t4" then test = "$t"

what I am expecting it to do is test to be "$t4", this works for every other $tX except for specifically the number 4 even though it's not in the (" ,.\t"+to_string(guide[i+1])) string

1 Answers
s.find_first_of(" ,.\t" + std::to_string(guide[i + 1]))

Assuming ASCII, that string will be:

 ,.\t44

44 is the ASCII value of the , in guide[i + 1]. The first character in "$t4," that it'll find is 4 at position 2, and you then create a substring from 0 and length 2, that is $t.

Related