Add the " symbol in a QStringList [duplicate] - c++

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 << '"';

Related

Replace preprocessor macro of string literal with concatenation [duplicate]

This question already has answers here:
Simplest way to combine two strings at compile time with C++11
(2 answers)
How to concatenate static strings at compile time?
(2 answers)
Concatenate compile-time strings in a template at compile time?
(4 answers)
Closed 8 months ago.
I am attempting to replace as many uses of macros in our code with proper c++17 constructs. How would I replace the following macro with a constexpr or something else?
#define FNAME "first"
#define LNAME "last"
#define NAME FNAME LNAME
const char hello[] = "hello " NAME;

C++ raw string with special char [duplicate]

This question already has answers here:
escape R"()" in a raw string in C++
(2 answers)
Include )" in raw string literal without terminating said literal
(3 answers)
Closed 9 months ago.
I want to output a string like this: onclick="func()". So I wrote the following code:
std::string s = R"(
onclick="func()"
)";
But here two )" let the compiler confused.
Please forgive me if it's a silly question.
I googled but found nothing (I don't know which keyword I should use when I googled).
Simply add a unique string outside the ()
std::string s = R"anystring(
onclick="func()"
)anystring";

Use the " character in a string [duplicate]

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"(")";

Some questions with printing output in C++ [duplicate]

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).

How to get last character of string in c++? [duplicate]

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();