Sometimes we want to preserve the string format especially when we use string in a string with /”. In C++14 and above, there is a std::quoted
template that allows handling strings safely where they may contain spaces and special characters and it keeps their formatting intact. In this post, we explain std::quoted
quoted strings in modern C++.
What Is Quoted String In Modern C++?
The std::quoted
template is included in <iomanip>
header, and it is used to handle strings (i.e. “Let’s learn from \”LearnCPlusPlus.org!\” “) safely where they may contain spaces and special characters and it keeps their formatting intact.
Here is the syntax,
1 2 3 |
std::quoted( <basic_string> ) |
Here are C++14 templates defined in <iomanip>
where the string is used as input,
1 2 3 4 5 |
template< class CharT > quoted( const CharT* s, CharT delim = CharT('"'), CharT escape = CharT('\\') ); |
or
1 2 3 4 5 6 |
template< class CharT, class Traits, class Allocator > quoted( const std::basic_string<CharT, Traits, Allocator>& s, CharT delim = CharT('"'), CharT escape = CharT('\\') ); |
Here is a C++14 template defined in <iomanip>
where the string is used as output,
1 2 3 4 5 6 |
template< class CharT, class Traits, class Allocator > quoted( std::basic_string< CharT, Traits, Allocator>& s, CharT delim=CharT('"'), CharT escape=CharT('\\') ); |
Note that this feature is is using std::basic_string
and it improved in C++17 by the std::basic_string_view
support.
What Is std::quoted quoted string in modern C++?
Here is an example that uses input string and outputs into a stringstream
:
1 2 3 4 5 |
const std::string str = "I say \"LearnCPlusPlus!\""; std::stringstream sstr; sstr << std::quoted(str); // input str to sstr |
Here is an example that uses stringstream
as an input and outputs into a string.
1 2 3 4 |
std::string str_out; sstr >> std::quoted(str_out); // output sstr to str_out |
Is there a full example about std::quoted quoted string in modern C++?
Here is a full example about std::quoted
in modern C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> #include <sstream> #include <iomanip> int main() { std::stringstream sstr; const std::string str = "Let's learn from \"LearnCPlusPlus.org!\" "; sstr << std::quoted(str); // input str to sstr std::cout << str << std::endl; std::cout << sstr.str() << std::endl; std::string str_out; sstr >> std::quoted(str_out); // output sstr to str_out std::cout << str_out << std::endl; system("pause"); return 0; } int main() { constexpr char c = checksize(8); std::cout << c << std::endl; system("pause"); return 0; } |
For more information about this feature, please see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3654.html
C++ Builder is the easiest and fastest C and C++ IDE for building powerful, good-looking applications. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition