Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define std::string in C++ without escape characters

What would be the most painless way to input un-escaped characters sequence to std::string?

Instead of

 std::string st = "C:\\program files\\myFile.txt";

something like: (Doesn't work correctly due to escape characters).

 std::string st = "C:\program files\myFile.txt";
like image 413
Andrey Rubshtein Avatar asked May 08 '12 15:05

Andrey Rubshtein


People also ask

How do I print a string without escape characters?

Using “r/R” Adding “r” or “R” to the target string triggers a repr() to the string internally and stops from the resolution of escape characters.

How do you avoid escape characters in C++?

Your string would look like this: string str[]={"| | / / ||", "| |/ / ||", "| | ( \\ \\ \\ \\`_."}; In C++ (and most other C-based langauges), \ is used to indicate an escape character (e.g. \r , \n , \t , etc). If you want to have an actual "\" character in your string, you must escape it: \\ .

Is there std::string in C?

The std::string class manages the underlying storage for you, storing your strings in a contiguous manner. You can get access to this underlying buffer using the c_str() member function, which will return a pointer to null-terminated char array. This allows std::string to interoperate with C-string APIs.


1 Answers

You can do it with C++11 raw string literals:

std::string st = R"(C:\program files\myFile.txt)";
like image 66
juanchopanza Avatar answered Sep 29 '22 01:09

juanchopanza