I have an HTML data in a char* and I would like to get it line by line, do some replacements and then add them all up together into a single string. This is the code that I use
std::string to, finalData;
finalData = "";
char* char_array = strtok(data, "\n");
while(char_array){
finalData += std::string(char_array);
char_array = strtok(NULL, "\n");
}
The problem is the data that I get at the end of this (finalData) has a lot of ^M characters and I am unable to search for it as it has a special character. Is there any way to completely eliminate the character?
I am guessing that it has something to do with conversion from c array to c++ string and to do with \n as tab is represented by ^I and cntrl is represented as ^
It seems that you are on a Windows system, or that the data originated on a Windows system. On a Windows system, newline is actually two characters:
"\r\n". What you are seeing as^Mis the carriage-return character ('\r') of that newline sequence.One way to remove those extra characters, would be to use
std::string::findandstd::string::erasein a loop.Another way would be to manually copy, character by character, to a new
std::string, except if the character is'\r'.