std::quoted
Min standard notice:
Header: <iomanip>
Allows insertion and extraction of quoted strings, such as the ones found in CSV or XML.
# Declarations
template< class CharT >
/*unspecified*/ quoted( const CharT* s,
CharT delim = CharT('"'), CharT escape = CharT('\\') );
(since C++14)
template< class CharT, class Traits, class Allocator >
/*unspecified*/ quoted( const std::basic_string<CharT, Traits, Allocator>& s,
CharT delim = CharT('"'), CharT escape = CharT('\\') );
(since C++14)
template< class CharT, class Traits>
/*unspecified*/ quoted( std::basic_string_view<CharT, Traits> s,
CharT delim = CharT('"'), CharT escape = CharT('\\') );
(since C++17)
template< class CharT, class Traits, class Allocator >
/*unspecified*/ quoted( std::basic_string<CharT, Traits, Allocator>& s,
CharT delim=CharT('"'), CharT escape=CharT('\\') );
(since C++14)
# Parameters
s: the string to insert or extractdelim: the character to use as the delimiter, defaults to "escape: the character to use as the escape character, defaults to \
# Return value
Returns an object of unspecified type such that the described behavior takes place.
# Notes
Feature-test macro Value Std Feature __cpp_lib_quoted_string_io 201304L (C++14) std::quoted
# Example
#include <iomanip>
#include <iostream>
#include <sstream>
void default_delimiter()
{
const std::string in = "std::quoted() quotes this string and embedded \"quotes\" too";
std::stringstream ss;
ss << std::quoted(in);
std::string out;
ss >> std::quoted(out);
std::cout << "Default delimiter case:\n"
"read in [" << in << "]\n"
"stored as [" << ss.str() << "]\n"
"written out [" << out << "]\n\n";
}
void custom_delimiter()
{
const char delim{'$'};
const char escape{'%'};
const std::string in = "std::quoted() quotes this string and embedded $quotes$ $too";
std::stringstream ss;
ss << std::quoted(in, delim, escape);
std::string out;
ss >> std::quoted(out, delim, escape);
std::cout << "Custom delimiter case:\n"
"read in [" << in << "]\n"
"stored as [" << ss.str() << "]\n"
"written out [" << out << "]\n\n";
}
int main()
{
default_delimiter();
custom_delimiter();
}