2

I have a simple problem: I want to use the .find method for searching for the " Symbol in a text file. Since you normally write example.find("a") for example, it's not possible to search for the " Symbol. Any advice how I can fix this?

Alex K.
  • 171,639
  • 30
  • 264
  • 288
gamma
  • 157
  • 5

3 Answers3

3

I'm assuming that you are referring to the C++ language, since that's what your title is called and you've tagged c++ with your question.

Simply, the solution is to do this:

    example.find("\""); // The markup shows that this is a valid line of code. See the line below this one to compare the 2.

This is because when you would do just this:

    example.find("""); // You can already see here using the marking how this will cause a compilation error

The compiler would see the first 2 apostrophes as a string in itself; an empty string. However, the third apostrophe would cause a problem as there would be no terminating apostrophe for it, which will cause a compilation error.

Simply to avoid these problems, we use the example.find("\""); notation to represent the apostrophe. This is the proper syntax for finding the " symbol as it avoids any conflicts with the other syntax rules of C++. This syntax is often referred to as an escape literal, named as such because it "escapes" a conflict with the general c++ syntax, avoiding a compilation error just as I said above.

On a side note, you will often encounter the backslash () in many other scenarios. For example, consider the following other examples of escape sequences: * \n * \t * \a * etc...

These are also escape literals, because without the backsash, they are simply processed as chars or strings. However, with the backslash character, they serve another purpose such as a new line, a tab key, etc...

Hope this answers your question.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
  • @gamma Here's how you can appreciate my answer: [__"What should I do if someone accepts my answer?"__](http://stackoverflow.com/help/someone-answers) :-) – BusyProgrammer Apr 09 '17 at 23:04
1

It is hard to answer the question without knowing what language you are in.

However, their is a chance that you can use backslash escaping. For example you would write example.find("\"").

user3639005
  • 323
  • 1
  • 2
  • 8
  • I'm sorry, I thought I've postet the question in C++. I will change it immediately. Anyhow I think the last user answered my problem so thank you. – gamma Jan 24 '17 at 00:09
0

Please have look at it, It will help for sure, Link for C++ , and link for Java .

One golden tip, include language in tag .

Community
  • 1
  • 1
Neer Patel
  • 399
  • 3
  • 11