简体   繁体   中英

Recognizing special characters in C++

I'm currently writing a program and would like to use the find() function and see if a string has a \\ in it. Whenever I try to use, eg token.find("\\", 0), I get an error saying that I'm missing a terminating " character. I'm assuming that this is because \\ is a special character. I've Googled and searched SO for a way to recognize special characters, like \\, and thought adding single quotes around the \\, ie "'\\'", might work, but it hasn't. Can someone please point me in the right direction or propose an alternative?

This is because "\\" is an escape character. To resolve this change the code to: token.find("\\\\", 0);

Use the find() method like this and it will. Work

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

As defined in the commet @Rafael Rotelok

if you need to use special characters on a search term you need to escape them with '\\' first, so if you are looking for the '\\' char you escape it with '\\', the first '\\' have a special meaning and says to your parser that you have to take the next '\\'char literally –

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