I want to put this:
<script src = "Script2.js" type = "text/javascript"> < / script>
in a std::string
so I append a (\
) symbol before every double quotes ("
) to give it a literal meaning of ", instead of a string demarcation in C++ like this:
std::string jsFilesImport = "<script src = \"Script2.js\" type = \"text/javascript\"> < / script>\""
If I have a big string
with many ("
), adding (\
) for every ("
) becomes difficult. Is there a simple way to achieve this in C++?
The easiest way is to use a raw string literal:
std::string s = R"x(<script src = "Script2.js" type = "text/javascript"> < / script>)x";
// ^^^^ ^^^
You just need to take care that the x(
)x
delimiters aren't used in the text provided inside. All other characters appearing within these delimiters are rendered as is (including newlines). x
is arbitrarily chosen, could be something(
, )something
as well.
To prevent further questions regarding this:
No, it's not possible to do1 something like
std::string s = R"resource(
#include "MyRawTextResource.txt"
)resource";
The preprocessor won't recognize the #include "MyRawTextResource.txt"
statement, because it's enclosed within a pair of double quotes ("
).
For such case consider to introduce your custom pre-build step doing the text replacement before compilation.
1)At least I wasn't able to find a workaround. Appreciate if someone proves me wrong about that.