Find escaped characters in a string line

2019-08-11 15:32发布

问题:

Using the std library, and the function find, how can I know if there are any escaped characters in the given string ?

For instance :

string line = "bla bla bla \n blabla";
bool hasEscapedSequence = (line.find("\n",0) < line.size());

This obviously won't work since the \n in the find will be escaped. If I try (line.find("\\n",0) < line.size());, it doesn't seem to change anything

How should I do ?

回答1:

line.find("\n",0)

Will return the position of the cursor where it will find the substring. If the substring is not found it will return string::npos. And so it should not be compared with the size.

bool hasEscapedSequence = (line.find("\n") != string::npos);