简体   繁体   中英

How to find the character “\” in a string?

I am trying to manipulate a string by finding the \\ character in the string Find\\inHere. However, I can't put that as an input in test.find('\\', 0) . It won't work and gives me the error "missing terminating character." Is there a way to fix test.find('\\', 0) ?

string test = "Find\inHere";

int x = test.find('\', 0);   // error on this line
cout << x;   // x should equal 4

\\ is a character used to introduce special characters, for example \\n newline, \\xDB shows the ASCII character with hexadecimal number DB etc.

So, in order to search this special character, you have to escape it by adding another \\ , use:

test.find("\\",0);

EDIT : Also, in your first string, it is not written in it "Find\\inHere" but "Find" and an error because \\inHere isn't a special instruction. So, same way to avoid it, write "Find\\\\inHere".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM