This question already has answers here:
How can I get double quotes into a string literal?
(2 answers)
Closed 1 year ago.
Hello I would like to use the character "in a string variable like this:
std::string hello = """;
Is it possible to do such a thing or am I talking nonsense?
Thanks in advance!
Just escape it:
std::string hello = "\"";
You have several ways, including:
Escaping: "\""
Raw string: R"(")" (since C++11)
You can either use the escaped character like
std::string hello( "\"" );
or
std::string hello = "\"";
or use a constructor that accepts a character literal like
std::string hello( 1, '"' );
Or you can use even a raw string literal like
std::string hello( R"(")" );
or
std::string hello = R"(")";
Related
This question already has answers here:
Rules for C++ string literals escape character
(6 answers)
Closed 6 years ago.
I am trying to create a QStringList containing all punctuation signs.
How can I add the element " into it ?
You can use \ to escape the character ". The code may look like this:
QStringList foo;
foo << "\"";
An other option would be to construct a QString from a char declared between simple quotes ':
foo << QString('"');
Since the constructor isn't declared as explicit in documentation, this should also work with implicit conversion:
foo << '"';
I am trying to follow this answer: https://stackoverflow.com/a/32435076/5484426, but for std::wstring. So far, I have this:
std::wstring str = L"hello hi hello hi hello hi";
std::wregex remove = L"hi";
Now I want to do this: regex_replace(str,remove,"");
It doesn't look like regex_replace works for wstring though. How do I remove all instances of this string?
std::regex_replace certainly works for std::wstring, and all other specializations of std::basic_string. However, the replacement format string's character type must match the character type of the regex and input string. This means you need a wide replacement format string:
std::regex_replace(str, remove, L"");
Apart from that, the constructor of std::wregex taking a const wchar_t* is explicit, so your copy-initialization will not work. Also, this overload of std::regex_replace returns a new string with the replacements done rather than doing them in place, which is something to be aware of.
Here is an example with these points considered:
std::wstring str = L"hello hi hello hi hello hi";
std::wregex remove{L"hi"};
str = std::regex_replace(str, remove, L"");
This question already has answers here:
What is the type of a string literal in C++? [duplicate]
(2 answers)
Closed 7 years ago.
When I use :
std::cout << "Hello world ";
Which type is "Hello world" ?
Where does it stored , so I can get it out and work with it ?
For some reasons, I don't want to use something like :
std::string str = "Hello world";
std::cout << str;
Please help me, I searched an hour but still no answer.
The type of a string literal is "constant array of char", with as many elements as characters in the literal, plus one for a final null character. Other versions of string literals (wide, unicode) are arrays of other character types (wchar_t, char16_t etc.) (e.g. see here).
This question already has answers here:
Get the last element of a std::string
(4 answers)
Closed 7 years ago.
In python you can say print "String"[-1] and it would print be the last character, 'g'. Is there an equivalent for this in c++?
You can use string.back() to get a reference to the last character in the string. The last character of the string is the first character in the reversed string, so string.rbegin() will give you an iterator to the last character.
Use the back() function for std::string:
std::string str ("Some string");
cout << str.back()
Output:
g
For C strings, it is
String[strlen(String) - 1];
For C++ style strings, it is either
String.back();
*String.rbegin();
String[String.length() - 1];
You can use the function:
my_string.back();
If you want to output it, then:
#include <iostream>
std::cout << my_string.back();
This question already has an answer here:
Get last match with Boost::Regex
(1 answer)
Closed 9 years ago.
Somehow, I've failed to find out, how to put only the first occurrence or regular expression to string. I can create a regex object:
static const boost::regex e("<(From )?([A-Za-z0-9_]+)>(.*?)");
Now, I need to match ([A-Za-z0-9_]+) to std::string, say playername.
std::string chat_input("<Darker> Hello");
std::string playername = e.some_match_method(chat_input, 1); //Get contents of the second (...)
What have I missed?
What should be instead of some_match_method and what parameters should it take?
You can do something like this:
static const regex e("<(From )?([A-Za-z0-9_]+)>(.*?)");
string chat_input("<Darker> Hello");
smatch mr;
if (regex_search(begin(chat_input), end(chat_input), mr, e)
string playername = mr[2].str(); //Get contents of the second (...)
Please note that regex is part of C++11, so you don't need boost for it, unless your regular expression is complex (as C++11 and newer still has difficulties processing complex regular expressions).
I think what you're missing is that boost::regex is the regular expression, but it doesn't do the parsing against a given input. You need to actually use it as a parameter to boost::regex_search or boost::regex_match, which evaluate a string (or iterator pairs) against the regular expression.
static const boost::regex e("<(From )?([A-Za-z0-9_]+)>(.*?)");
std::string chat_input("<Darker> Hello");
boost::match_results<std::string::const_iterator> results;
if (boost::regex_match(chat_input, results, e))
{
std::string playername = results[2]; //Get contents of the second (...)
}