C ++字符串文字规则转义字符 [英] Rules for C++ string literals escape character

查看:142
本文介绍了C ++字符串文字规则转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串文字中的转义字符 \ 的规则是什么?有没有所有转义的字符的列表?

What are the rules for the escape character \ in string literals? Is there a list of all the characters that are escaped?

特别是当我使用 \ 字符串在gedit中的字符串,并按照任何三个数字,它颜色不同。

In particular, when I use \ in a string literal in gedit, and follow it by any three numbers, it colors them differently.

我正在尝试创建一个 std :: string 由字符 0 后跟空字符( \0 )的文字构建, ,后跟字符 0 。然而,语法高亮警告我,也许这将创建一个像$ 0 之后是空字符( \00 ,又名 \0 ),也就是说只有两个字符。

I was trying to create a std::string constructed from a literal with the character 0 followed by the null character (\0), followed by the character 0. However, the syntax highlighting alerted me that maybe this would create something like the character 0 followed by the null character (\00, aka \0), which is to say, only two characters.

只是这个问题,这是最好的方法:

For the solution to just this one problem, is this the best way to do it:

std::string ("0\0" "0", 3)  // String concatenation 

还有一些参考,转义字符在一般字符串文字例如,'\a'?

And is there some reference for what the escape character does in string literals in general? What is '\a', for instance?

推荐答案

控制字符

(十六进制代码假设ASCII兼容字符编码。)

(Hex codes assume an ASCII-compatible character encoding.)


  • \a = \x07 = alert(bell)

  • \b = \x08 =退格

  • \t = \x09 =水平选项卡

  • \\\
    = \x0A =换行符(或换行符)

  • \v = \x0B =垂直标签

  • \f = \x0C =表单Feed

  • \r = \x0D =回车

  • \e = \x1B = escape(非标准GCC扩展名)

  • \a = \x07 = alert (bell)
  • \b = \x08 = backspace
  • \t = \x09 = horizonal tab
  • \n = \x0A = newline (or line feed)
  • \v = \x0B = vertical tab
  • \f = \x0C = form feed
  • \r = \x0D = carriage return
  • \e = \x1B = escape (non-standard GCC extension)

标点符号:


  • \ =引号(反斜杠号''

  • \' =撇号')不需要反斜杠

  • \? = question标记(用于避免三角形)

  • \\ =反斜杠

  • \" = quotation mark (backslash not required for '"')
  • \' = apostrophe (backslash not required for "'")
  • \? = question mark (used to avoid trigraphs)
  • \\ = backslash

数字字符参考:


  • \\ +最多3个八位数字

  • \x +任意数字的十六进制数字

  • \u + 4 hex数字(Unicode BMP,C ++ 11中的新增)

  • \U +十六进制数字(Unicode星象,C ++ 11中新增)

  • \ + up to 3 octal digits
  • \x + any number of hex digits
  • \u + 4 hex digits (Unicode BMP, new in C++11)
  • \U + 8 hex digits (Unicode astral planes, new in C++11)

\0 = \00 = \000 =空字符的八进制ecape

\0 = \00 = \000 = octal ecape for null character

如果在 \0 ,那么是的,我推荐字符串连接。请注意,文字部分之间的空格是可选的,因此您可以写入\00

If you do want an actual digit character after a \0, then yes, I recommend string concatenation. Note that the whitespace between the parts of the literal is optional, so you can write "\0""0".

这篇关于C ++字符串文字规则转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆