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, e.g. 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 S.O. for a way to recognize special characters, like \, and thought adding single quotes around the \, i.e. "'\'", might work, but it hasn't. Can someone please point me in the right direction or propose an alternative?
Asked
Active
Viewed 300 times
-2
-
useful link: https://stackoverflow.com/questions/10220401/rules-for-c-string-literals-escape-character – Paul Rooney Jan 31 '19 at 02:36
-
1if 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 – Rafael Rotelok Jan 31 '19 at 02:38
-
You could make your question more clear by including a small example program (we call this an [MCVE](https://stackoverflow.com/help/mcve)). This often helps eliminate ambiguities (like for example are you using the `find` member function of `std::string`?). It could be as simple as [this](https://wandbox.org/permlink/R5R6aAhavU2XAoUq). – Paul Rooney Jan 31 '19 at 02:39
-
I will see if I can get a MCVE. This is for a programming project, so I'd like to not put all my code out there. Using "\\" has fixed the current issue, but at the same time, I'm getting a new error that I need to fix. – MattyT Jan 31 '19 at 02:43
-
are you using a syntax-highlighting editor? Do you realize that in `token.find("\", 0)` the string doesn't terminate at the end of the line – phuclv Jan 31 '19 at 02:45
-
possible duplicate: [using \ in a string as literal instead of an escape](https://stackoverflow.com/q/12103445/995714), [strcat(dest, "\something") - backslash not showing](https://stackoverflow.com/q/45400859/995714), [How to compare a string with symbol '\'](https://stackoverflow.com/q/28850657/995714)... – phuclv Jan 31 '19 at 02:47
-
1Yes, obviously my searching skills are not the best. I appreciate the links and tips to move in the right direction. – MattyT Jan 31 '19 at 02:49
2 Answers
0
This is because "\" is an escape character. To resolve this change the code to: token.find("\\", 0);

Rachel Casey
- 1
- 3
-
Ah, you said that it is ("\", 0), right now. For clarity do you have one escape character or two? – Rachel Casey Jan 31 '19 at 02:36
-
To find 1 ` \ ` char, you need to use 2 ` \\ ` chars in the string literal: `token.find("\\", 0);` – Remy Lebeau Jan 31 '19 at 02:38
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 –

Hamza.S
- 1,319
- 9
- 18