This question already has answers here:
Rules for C++ string literals escape character
(6 answers)
Closed 7 years ago.
I was trying to make a simple calculator and I would like to display quotes in the instruction when you first run the program.
Use \". Also known as escape sequences.
Another solution is to use raw strings:
#include <string>
#include <iostream>
int main()
{
std::cout << R"_(A raw string with " inside (and \ as well))_" << std::endl;
return 0;
}
Live example
Output:
A raw string with " inside (and \ as well)
Quotes from the Standard:
According to the Standard 2.14.5 [lex.string]:
string-literal:
encoding-prefixopt "s-char-sequenceopt"
encoding-prefixopt R raw-string
encoding-prefix:
u8
u
U
L
s-char-sequence:
s-char
s-char-sequence s-char
s-char:
any member of the source character set except
the double-quote ", backslash \, or new-line character
escape-sequence
universal-character-name
raw-string:
" d-char-sequenceopt ( r-char-sequenceopt) d-char-sequenceopt "
r-char-sequence:
r-char
r-char-sequence r-char
r-char:
any member of the source character set, except
a right parenthesis ) followed by the initial d-char-sequence
(which may be empty) followed by a double quote ".
d-char-sequence:
d-char
d-char-sequence d-char
d-char:
any member of the basic source character set except:
space, the left parenthesis (, the right parenthesis ), the backslash \,
and the control characters representing horizontal tab,
vertical tab, form feed, and newline.
A string literal is a sequence of characters (as defined in 2.14.3) surrounded by double quotes, optionally prefixed by R, u8, u8R,
u, uR, U, UR, L, or LR, as in "...", R"(...)",
u8"...", u8R"**(...)**", u"...", uR"*˜(...)*˜", U"...",
UR"zzz(...)zzz", L"...", or LR"(...)", respectively.
A string literal that has an R in the prefix is a raw string literal. The d-char-sequence serves as a delimiter. The terminating
d-char-sequence of a raw-string is the same sequence of characters
as the initial d-char-sequence. A d-char-sequence shall consist of
at most 16 characters.
[Note: The characters ( and ) are permitted in a raw-string. Thus, R"delimiter((a|b))delimiter" is equivalent to "(a|b)". —end
note ]
[Note: A source-file new-line in a raw string literal results in a new-line in the resulting execution string-literal. Assuming no
whitespace at the beginning of lines in the following example, the
assert will succeed:
const char *p = R"(a\
b
c)";
assert(std::strcmp(p, "a\\\nb\nc") == 0);
— end note ]
[Example: The raw string
R"a(
)\
a"
)a"
is equivalent to "\n)\\\na\"\n". The raw string
R"(??)"
is equivalent to "\?\?". The raw string
R"#(
)??="
)#"
is equivalent to "\n)\?\?=\"\n". —end example ]
Related
I have a QString with following content:
"MXTP24\\x00\\x00\\xF4\\xF9\\x80\r\n"
I want it to become:
"MXTP24\x00\x00\xF4\xF9\x80\r\n"
I need to replace the "\x" to "\x" so that I can start parsing the values. But the following code, which I think should do the job is not doing anything as I get the same string before and after:
qDebug() << "BEFORE: " << data;
data = data.replace("\\\\x", "\\x", Qt::CaseSensitivity::CaseInsensitive);
qDebug() << "AFTER: " << data;
Here, no change!
Then I tried like this:
data = data.replace("\\x", "\x", Qt::CaseSensitivity::CaseInsensitive);
Then compiler complaines that \x used with no following hex digits!
any ideas?
First let's look at what this piece of code does:
data.replace("\\\\x", "\\x", ....
First string becomes \\x in compiled code, and is used as regular expression. In reqular expression, backslash is special, and needs to be escaped with another backslash to mean actual single backslash character, and your regexp does just this. 4 backslashes in C+n string literal regexp means matching single literal backslash in target text. So your reqular expression matches literal 2-character string \x.
Then you replace it. Replacement isn't a reqular expression, so backslash doesn't need double escaping here, so you end up using literal 2-char replacement string \x, which is same as what you matched, so even if there is a match, nothing changes.
However, this is not your problem, your problem is how qDebug() prints strings. It prints them escaped. That \" at start of output means just plain double quote, 1 char, in the actual string because double quote is escaped. And those \\ also are single backslash char, because literal backslash is also escaped (because it is the escape char and has special meaning for the next char).
So it seems you don't need to do any search replace at all, just remove it.
Try printing the QString in one of these ways to get is shown literally:
std::cout << data << std::endl;
qDebug() << data.toLatin1().constData();
For a function I am making, I take a string in as a parameter and do things with it. However I treat characters in the string specially if there is a backslash before it. However I am having problems even seeing the blackslash!
std::string s = "01234\6";
std::cout << s << std::endl;
std::cout << s.at(5) << std::endl;
if(s.at(5)== '\\')
std::cout << "It's a backslash" << std::endl;
else
std::cout << "It's not a backslash" << std::endl;
outputs
01234
It's not a backslash
How am I supposed to check if mystring.at(i) == '\\' if it isn't showing up at all?
The input will be coming from another file (which I can't modify) like
myfunc("% \% %");
If I read the string I count 3 '%' characters (so its not ignored by the backslash), and 0 '\' characters
edit: Code how I count
char percent = '%';
int current_index = 0;
int percent_count = 0;
int ret = str.find(percent, current_index);
while(ret != std::string::npos)
{
percent_count++;
current_index = ret +1;
ret = str.find(percent, current_index);
}
return percent_count;
C++ supports three kinds of escape sequences:
simple-escape-sequence. It is one of:
\’ \" \? \\
\a \b \f \n \r \t \v
octal-escape-sequence. It is one of:
\ octal-digit
\ octal-digit octal-digit
\ octal-digit octal-digit octal-digit
\0 is the most well known octal escape sequence that represents the null character.
hexadecimal-escape-sequence. It is one of:
\x hexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit
When you use:
std::string s = "01234\6";
the \6 part represents an octal escape sequence. It does not represent two characters.
It is the same as
std::string s = "01234?";
where ? is the character represented by the octal number 6.
In order to have \ as an element of the string, you'll need to use:
std::string s = "01234\\6";
The checking method is right, but \ escape 6, so \6 is counted once, you can check sizeof("12345\6"), which 7, or strlen("12345\6"), which is 6.
Change "12345\6" to "12345\\6".
The C++ compiler would have already treated it specially if you have backslash in the string:
std::string s = "01234\6"; //\6 is treated differently already, as unicode character \6, not as backslash + 6
Unless what you mean is you want to have a text with backslash (say, from I/O). In that case, you should put \\ to make your compiler understand that you mean it as real backslash not a unicode character:
std::string s = "01234\\6"; //double backslash here
Then you can test your program.
No compiler C++ will interpret \ as a backslash, since its the escape character. You will have to use \\ to denote a backslash in a string.
I want to find the number between [/ and ] (12345 in this case).
I have written such code:
float num;
string line = "A111[/12345]";
boost::regex e ("[/([0-9]{5})]");
boost::smatch match;
if (boost::regex_search(line, match, e))
{
std::string s1(match[1].first, match[1].second);
num = boost::lexical_cast<float>(s1); //convert to float
cout << num << endl;
}
However, I get this error: The error occurred while parsing the regular expression fragment: '/([0-9]{5}>>>HERE>>>)]'.
You need to double escape the [ and ] that special characters in regex denoting character classes. The correct regex declaration will be
boost::regex e ("\\[/([0-9]{5})\\]");
This is necessary because C++ compiler also uses a backslash to escape entities like \n, and regex engine uses the backslash to escape special characters so that they are treated like literals. Thus, backslash gets doubled. When you need to match a literal backslash, you will have to use 4 of them (i.e. \\\\).
Use the following (escape [ and ] because they are special characters in regex meaning a character class):
\\[/([0-9]{5})\\]
^^ ^^
Aim: to read a string in the form First\nSecond from a file and to print it as
First
Second
Problem: if the string is defined in the code, as in line = "First\nSecond";, then it is printed on two lines; if instead I read it from a file then is printed as
First\nSecond
Short program illustrating the problem:
#include "stdafx.h" // I'm using Visual Studio 2008
#include <fstream>
#include <string>
#include <iostream>
void main() {
std::ifstream ParameterFile( "parameters.par" ) ;
std::string line ;
getline (ParameterFile, line) ;
std::cout << line << std::endl ;
line = "First\nSecond";
std::cout << line << std::endl ;
return;
}
The parameters.par file contains only the line
First\nSecond
The Win32 console output is
C:\blabla>SOtest.exe
First\nSecond
First
Second
Any suggestion?
In C/C++ string literals ("...") the backslash is used to mark so called "escape sequences" for special characters. The compiler translates (replaces) the two characters '\' (ASCII code 92) followed by 'n' (ASCII code 110) by the new-line character (ASCII code 10). In a text file one would normally just hit the [RETURN] key to insert a newline character. If you really need to process input containing the two characters '\' and 'n' and want to handle them like a C/C++ compiler then you must explicitely replace them by the newline character:
replace(line, "\\n", "\n");
where you have to supply a replace function like this:
Replace part of a string with another string (Standard C++ does not supply such a replace function by itself.)
Other escape sequences supported by C/C++ and similar compilers:
\t -> [TAB]
\" -> " (to distinguish from a plain ", which marks the end of a string literal, but is not part of the string itself!)
\\ -> \ (to allow having a backslash in a string literal; a single backslash starts an escape sequence)
The character indicated in a string literal by the escape sequence \n is not the same as the sequence of characters that looks like \n!
When you think you're assigning First\nSecond, you're not. In your source code, \n in a string literal is a "shortcut" for the invisible newline character. The string does not contain \n - it contains the newline character. It's automatically converted for you.
Whereas what you're reading from your file is the actual characters \ and n.
I would like to know is it possbile to return "weird" characters, or rather ones that are important to the language
For example: \ ; '
I would like to know that because I need to return them by one function that's checking the unicode value of the text key, and is returning the character by it's number, I need these too.
I get a 356|error: missing terminating ' character
Line 356 looks as following
return '\';
Ideas?
The backslash is an escape for special characters. If you want a literal backslash you have to escape it with another backslash. Try:
return '\\';
The only problem here is that a backslash is used to escape characters in a literal. For example \n is a new line, \t is a horizontal tab. In your case, the compiler is seeing \' and thinking you mean a ' character (this is so you could have the ' character like so: '\''). You just need to escape your backslash:
return '\\';
Despite this looking like a character literal with two characters in it, it's not. \\ is an escape sequence which represents a single backslash.
Similarly, to return a ', you would do:
return '\'';
The list of available escape sequences are given by Table 7:
You can have a character literal containing any character from the execution character set and the resulting char will have the value of that character. However, if the value does not fit in a char, it will have implementation-defined value.
Any character can be returned.
Yet for some of them, you have to escape it using backslash: \.
So for returning backslash, you have to return:
return '\\';
To get a plain backslash use '\\'.
In C the following characters are represented using a backslash:
\a or \A : A bell
\b or \B : A backspace
\f or \F : A formfeed
\n or \N : A new line
\r or \R : A carriage return
\t or \T : A horizontal tab
\v or \V : A vertical tab
\xhh or \Xhh : A hexadecimal bit pattern
\ooo : An octal bit pattern
\0 : A null character
\" : The " character
\' : The ' character
\\ : A backslash (\)
A plain backslash confuses the system because it expects a character to follow it. Thus, you need to "escape" it. The octal/hexadecimal bit patterns may not seem too useful at first, but they let you use ANSI escape codes.
If the character following the backslash does not specify a legal escape sequence, as shown above, the result is implementation defined, but often the character following the backslash is taken literally, as though the escape were not present.
If you have to return such characters(",',\,{,]...etc) more then once, you should write a function that escapes that characters. I wrote that function once and it is:
function EscapeSpecialChars (_data) {
try {
if (!GUI_HELPER.NOU(_data)) {
return _data;
}
if (typeof (_data) != typeof (Array)) {
return _data;
}
while (_data.indexOf("
") > 0) {
_data = _data.replace("
", "");
}
while (_data.indexOf("\n") > 0) {
_data = _data.replace("\n", "\\n");
}
while (_data.indexOf("\r") > 0) {
_data = _data.replace("\r", "\\r");
}
while (_data.indexOf("\t") > 0) {
_data = _data.replace("\t", "\\t");
}
while (_data.indexOf("\b") > 0) {
_data = _data.replace("\b", "\\b");
}
while (_data.indexOf("\f") > 0) {
_data = _data.replace("\f", "\\f");
}
return _data;
} catch (err) {
alert(err);
}
},
then use it like this:
return EscapeSpecialChars("\'"{[}]");
You should improve the function. It was working for me, but it is not escaping all special characters.