[SOLVED] Why is string.find_first_of behaving this way?

Issue

This Content is from Stack Overflow. Question asked by Different

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



Solution

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.


This Question was asked in StackOverflow by Different and Answered by Ted Lyngmo It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?