Suppose I have this kind of string format:
"<RGB:255,0,0>this text is colored RED.<RGB:0,255,0> While this text is colored GREEN"; I want to extract the values inside the <RGB> i.e 255,0,0 and put it on other variables then delete the chars from '<' to '>'.
My code so far:
//this function is called after the loop that checks for the existence of '<' void RGB_ExtractAndDelete(std::string& RGBformat, int index, RGB& rgb) { int i = index + 5; //we are now next to character ':' std::string value; int toNumber; while (RGBformat[i] != ',') { value += RGBformat[i++]; } ++i; std::stringstream(value) >> toNumber; rgb.R = toNumber; value = ""; while (RGBformat[i] != ',') { value += RGBformat[i++]; } ++i; std::stringstream(value) >> toNumber; value = ""; rgb.G = toNumber; while (RGBformat[i] != '>') { value += RGBformat[i++]; } ++i; std::stringstream(value) >> toNumber; value = ""; rgb.B = toNumber; //I got the right result here which is //start: <, end: > printf("start: %c, end: %c\n", RGBformat[index], RGBformat[i]); //but fail in this one //this one should erase from '<' until it finds '>' RGBformat.erase(index, i); } If I put the <RGB:?,?,?> on the start of the string, it works but it fails when it finds it next to a non '<' character. Or can you suggest much better approach how to do this?
second<RGB:?,?,?>