Auto Escaping C++ Strings

Just a few weeks after celebrating C++’s  raw strings.  I found myself writing  regular expressions  in Visual Studio 2012. I was disappointed to find out VS 2012  it did not support raw strings.     It’s streaking just how half-baked VS 2012  is.  I think it safe the assume Microsoft Visual Studio’s marking team insisted  on sticking to a 2-year product release cycle while the technical team protested they need more time to implement the then new C++ 11 standard.  The compromise  was a product with only some of the exciting  new  C++  features in place.  The rest had to wait for VS 2013/2015.   As it turned out one of the casualties were raw strings.

Regex are difficult to read on their on,  writing and reading long escaped regex is a form of torture.   Fortunately the escaping and unescaping  parts follow  simple rules can be done automatically.  provides tools to escape and unescape a C++ string:


The example above escapes a regex matching a positive float enclosed by double quotes:

“\d*\.?\d+”  ->   “\\\d*\\.?\\d+\”


When forced to escape C++ string manually, it’s best to leave this daunting task to a computer  program which will do a faster, better job with less mistake.

Or, be more vocal about upgrading to a newer compiler.

Raw Strings in C++


Sometimes Strings in C++  must be pampered and gently persuaded to display correctly.  Consider a string representing a small HTML block :

<h1 style="color: #5e9ca0;"><strong>welcome!</strong></h1>
<h4 style="color: #5e9ca0;">these are the available options:</h4>
	<li>Reset Sensor</li>
	<li>Disengage power coupling</li>

generating this output :


these are the available options:

  1. Reset Sensor
  2. Disengage power coupling

if I  want to keep the HTML block in a string  literal,  I  can’t just copy and paste it from  a text editor (where I presumably created and tested it) . It will have to escaped it first, and then either split  to adjoining static strings (or have a very long one-line string):

std::string htmlMessage =  "<h1 style=\"color: #5e9ca0;\"><strong>welcome!</strong></h1>\n"
"<h4 style=\"color: #5e9ca0;\">these are the available options:</h4>\n"
"<li>Reset Sensor</li>\n"
"<li>Disengage power coupling</li>\n"

Fortunately, with C++ 11  and up this  kind of manual pre-processing can be a thing of the past:

 std::string htmlMessage = R"(<h1 style="color: #5e9ca0;"><strong>welcome!</strong></h1>
  <h4 style="color: #5e9ca0;">these are the available options:</h4>
	  <li>Reset Sensor</li>
	  <li>Disengage power coupling</li>

The unusual form R”(<actual string>)”  is the raw string modifier. When encountering  a raw string, the compiler will treat all characters in the string literal as simple  plain characters including normally escaped characters  . For example,  It will not attempt to translate ‘\t‘  into a tab character,  instead, the string literal will include the actual sequence “\t

Note how the left and right parenthesis inside the quotes are part of the raw string literal.

By having this slightly more complex definition  the raw string can include the ‘ ‘    character itself inside the string without confusing the compiler into believing  the string has terminated.

I am sure at least one of you ask: What if I want to include the  literal  )”  in my string ?     That standard has  you covered by having an optional delimiter string  at both ends of the string

So you can define the string as :

std::string rowString = R”C++Island(look ma “( and “) in a raw string!)C++Island!”

here the string  C++Island  comes between  the ‘“‘ and ‘(‘ or ‘)’   indicating to  the compiler to be out on the lookout for this specific  sequence signaling  the literal end.

The delimiter string can be anything you want as long as its 16 characters at most.

C++ raw string’s power become’s its  weakness when trying to express special character such as newline ‘\n‘ or tab ‘\t‘. However, you can always fall back on the old familiar  none-raw strings   you can also contact the two literals types together :

std::string str = R"(The '\n' character can easily be shown)" "\n"  "and used";
std::cout << str;

The ‘\n’ character can easily be shown
and used
[/color-box color]


While not being a game changer in C++ it’s nice having the language  do the boring technical stuff  for me instead of the other way around.  I mean isn’t  that what software development is all about ?   plus all the other kids languages have  this feature, it about time we get to play with it as well.