What is the C++ equivalent of the C# @ symbol prefixing strings? For escaping symbols automatically?
Example: var howManySlashesAreThereIn = @"\\\\\\"
;
What is the C++ equivalent of the C# @ symbol prefixing strings? For escaping symbols automatically?
Example: var howManySlashesAreThereIn = @"\\\\\\"
;
You're looking for the "raw string" feature of C++ but it's a fairly recent addition (C++11, I believe).
std::string howManySlashesAreThereIn = R"(\\\\\\)";
It's documented in 2.14.5 String lieterals
of the C++11 standard, though that tome may be a little dry for you, so I'd suggest googling up on "raw string" c++
.
As well as getting rid of those escape doubling monstrosites like "\\\\nasbox\\sharename\\downloads\\super_sekrit_stuff"
, it also allows you to bypass the "adding \n
characters and combining adjacent strings" trick such as turning:
htmlString =
"<HTML>\n"
"<HEAD>\n"
"<TITLE>My page</TITLE>\n"
"<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=utf-8\">\n"
"</HEAD>\n"
"<BODY LINK=\"#0000ff\" VLINK=\"#800080\" BGCOLOR=\"#ffffff\">\n"
"<P> </P>\n"
"<PRE>\n";
into something more readable (not exactly the same due to spaces in the second variant, but perfectly okay for HTML since it ignores the added spaces in this case):
htmlString = R"xyzzy(
<HTML>
<HEAD>
<TITLE>My page</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</HEAD>
<BODY LINK="#0000ff" VLINK="#800080" BGCOLOR="#ffffff">
<P> </P>
<PRE>
)xyzzy";
C++11 adds raw
string literals that are at least somewhat similar.
R"(This is a raw literal)";
These are particularly useful for regexes, like:
R"@(\w+\d*)@"
...which would, as a traditional literal would be:
"\\w+\\d*"
Although the difference isn't huge, it can make a difference, especially in a longer regex.