1

I am trying to read a backslash as part of a string, but it is always escaped.

For example:

string s = "Bo\nes"

for(int i = 0; i < s.size(); i++) {
    if (s[i] == '\\') cout << "Found a backslash";
}

That does not work. And across other testing I always get the string reading as "Boes". How would I be able to read that backslash such that I can parse the entire string?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
wirly
  • 27
  • 6
  • 1
    This may help as well as the answer: [https://stackoverflow.com/questions/56710024/what-is-a-raw-string](https://stackoverflow.com/questions/56710024/what-is-a-raw-string) – drescherjm Apr 01 '21 at 22:42
  • 1
    "_That does not work_" - In the future: In what way? When I read the code, I get what *I* am expecting. Please include your expected output in the question. – Ted Lyngmo Apr 02 '21 at 00:23

3 Answers3

6

Here are examples

std::string s1 = "Bo\\nes";
std::string s2 = R"(Bo\nes)";

That is either you have to use the escape sequence '\\' for the backslash character or a raw string literal.

Here is a demonstrative program.

#include <iostream>

int main() 
{
    std::string s1 = "Bo\\nes";
    std::string s2 = R"(Bo\nes)";
    
    std::cout << "\"" << s1 << "\"\n";
    std::cout << "\"" << s2 << "\"\n";
    
    return 0;
}

Its output is

"Bo\nes"
"Bo\nes"

Another way is to use an octal or hexadecimal escape sequence like for example

std::string s = "Bo\134nes";

or

std::string s = "Bo\x5Cnes";
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I'm given an input string (like "Bo\nes") and I need to be able to parse the character after the initial '\' (to escape it as part of the program). Do you know if there's a way to convert a string into a raw string so I'm able to do this? – wirly Apr 01 '21 at 23:02
  • 1
    @wirly It is unclear what is the problem/ If you entered a string like this Bo\nes from a console then this expression s[i] == '\\' is correct. – Vlad from Moscow Apr 01 '21 at 23:06
  • Unfortunately I'm working from a string being given to a class, and the input doesn't use double backslashes to escape the character – wirly Apr 01 '21 at 23:58
  • It is implementation-defined whether `'\\'` and `'\134'` (or `'\x5C'`) are equivalent. While that is true in ANSI (or compatible) character sets, it is not true for all possible character sets. – Peter Apr 02 '21 at 00:20
  • @wirly - The escaping of a backslash is only needed in source code (in string or character literals). If the user types `A\A` and that is read by the program (e.g. from `std::cin`) to a `std::string` named `input` then that character will be detected using logic such as `if (input == "A\\A") ...` – Peter Apr 02 '21 at 00:24
5

You'll have to escape the backslash in the original string.

string s = "Bo\\nes";

Otherwise, there is no backslash character in the string to compare against.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

In a string/character literal, \n is an escape sequence for a line feed (decimal 10, hex 0x0A), and \\ is an escape sequence for a single \ character (decimal 92, hex 0x5C).

Thus, in the string literal "Bo\nes", there is no \ character in the string. It contains these characters:

B o \n e s (0x42 0x6F 0x0A 0x64 0x73)

Not these characters, like you are expecting:

B o \ n e s (0x42 0x6F 0x5C 0x6E 0x64 0x73)

That is why if (s[i] == '\\') (aka if (s[i] == 0x5C)) is always false.

You need to escape the \ in \n to accomplish what you want:

string s = "Bo\\nes";

Then the literal will contain the characters you are expecting.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770