首页 > 其他分享 >Raw String Literals

Raw String Literals

时间:2023-07-20 15:55:57浏览次数:36  
标签:raw Literals String sequence char Raw literal const string

Raw string literals are string literals that can span multiple lines of code, they don’t require escaping
of embedded double quotes, and escape sequences like \t and \n are processed as normal text and
not as escape sequences. Escape sequences are discussed in Chapter 1, “A Crash Course in C++ and
the Standard Library.” For example, if you write the following with a normal string literal, you will
get a compilation error because the string contains non-escaped double quotes:
  const char* str { "Hello "World"!" }; // Error!

Normally you have to escape the double quotes as follows:
  const char* str { "Hello \"World\"!" };
With a raw string literal, you can avoid the need to escape the quotes. A raw string literal starts with
R"( and ends with )":
  const char* str { R"(Hello "World"!)" };
If you need a string consisting of multiple lines, without raw string literals, you need to embed \n
escape sequences in your string where you want to start a new line. Here’s an example:
  const char* str { "Line 1\nLine 2" };
If you output this string to the console, you get the following:
  Line 1
  Line 2
With a raw string literal, instead of using \n escape sequences to start new lines, you can simply press
Enter to start real physical new lines in your source code as follows. The output is the same as the
previous code snippet using the embedded \n.
  const char* str { R"(Line 1
  Line 2)" };
Escape sequences are ignored in raw string literals. For example, in the following raw string literal,
the \t escape sequence is not replaced with a tab character but is kept as the sequence of a backslash
followed by the letter t:
  const char* str { R"(Is the following a tab character? \t)" };
So, if you output this string to the console, you get this:
  Is the following a tab character? \t
Because a raw string literal ends with )", you cannot embed a )" in your string using this syntax.
For example, the following string is not valid because it contains the )" sequence in the middle of
the string:
  const char* str { R"(Embedded )" characters)" }; // Error!
If you need embedded )" characters, you need to use the extended raw string literal syntax, which is
as follows:
  R"d-char-sequence(r-char-sequence)d-char-sequence"
The r-char-sequence is the actual raw string. The d-char-sequence is an optional delimiter
sequence, which should be the same at the beginning and at the end of the raw string literal. This
delimiter sequence can have at most 16 characters. You should choose this delimiter sequence as a
sequence that will not appear in the middle of your raw string literal.
The previous example can be rewritten using a unique delimiter sequence as follows:
  const char* str { R"-(Embedded )" characters)-" };
Raw string literals make it easier to work with database querying strings, regular expressions, file
paths, and so on. Regular expressions are discussed in Chapter 21, “String Localization and Regular
Expressions.”

标签:raw,Literals,String,sequence,char,Raw,literal,const,string
From: https://www.cnblogs.com/blizzard8204/p/17568621.html

相关文章

  • strings
    strings在对象文件或二进制文件中查找可打印的字符串补充说明strings命令在对象文件或二进制文件中查找可打印的字符串。字符串是4个或更多可打印字符的任意序列,以换行符或空字符结束。strings命令对识别随机对象文件很有用。语法strings[-a][-][-o][-tFormat......
  • python string 处理
    PythonString处理在Python编程语言中,字符串(String)是一种常用的数据类型。字符串是一串由字符组成的数据,可以用于存储和表示文字、数字和特殊字符等。Python提供了丰富的内置函数和方法,用于处理和操作字符串。本文将介绍一些常用的Python字符串处理方法和技巧,并提供相应的代码示例......
  • [LeetCode] 2486. Append Characters to String to Make Subsequence
    Youaregiventwostrings s and t consistingofonlylowercaseEnglishletters.Return theminimumnumberofcharactersthatneedtobeappendedtotheendof s sothat t becomesa subsequence of s.A subsequence isastringthatcanbederived......
  • C# 后端请求 PostAsync GetStringAsync
     stringsendUrl=$"http://10.172.1.20/wtoptst/ws/r/awsp920";HttpClientsendclient=newHttpClient();stringtestStr=JsonConvert.SerializeObject(reques);//查看内容(测试用)HttpConten......
  • String
    一、概述 二、创建方式 三、内存模型 栈和方法有关堆和new(对象)有关方法区存放的是class形式的文件ps:JDK7以后有了一个StringTable(串池)专门在存储字符串;例:直接用双引号赋值优点:效率高,节省内存(因为如果该字符串在串池中已经存在,就不会再创建新的字符串,而是进行复用)......
  • MSSQL STRING_SPLIT(把字符串拆分成集合)
    语法:STRING_SPLIT(string,separator)参数说明:string:任何字符类型(例如nvarchar、varchar、nchar或char)的表达式separator:任何字符类型(例如nvarchar(1)、varchar(1)、nchar(1)或char(1))的单字符表达式,用作串联子字符串的分隔符根据字符把字符串拆分为集合S......
  • 数据库PostgreSQL PG 字符串拼接,大小写转换,substring
    前言PostgreSQL数据库简称pg数据库。本文主要介绍使用pg数据库时,字符串的一些常用操作。例如:多个字符串如何连接在一起,字符串如何大小写转换,删除字符串两边的空格,查找字符位置,查找子字符串等。一、多个字符串如何连接,拼接?pg的字符串连接使用||,注意不是+1.将2个字符串hello......
  • TextOut 与DrawText
    TextOut与DrawText1、CDC::TextOut()在指定位置输出指定字符串。函数原型:virtualBOOLTextOut(intx,inty,LPCTSTRlpszString,intnCount);BOOLTextOut(intx,inty,constCString&str);CPaintDCdc(this);dc.TextOut(0,50,_T("helloworld"));2、CDC::DrawText()......
  • RAW算法处理之BLC(Black level Correction黑电平校正)
    BL产生的原因暗电流暗电流(darkcurrent),也称无照电流,指在没有光照射的状态下,在太阳电池、光敏二极管、光导电元件、光电管等的受光元件中流动的电流,一般由于载流子的扩散或者器件内部缺陷造成。目前常用的CMOS就是光电器件,所以也会有暗电流,导致光照为0的时候也有电压输出。如......
  • 深入解析 C++ 中的 ostringstream、istringstream 和 stringstream 用法
    引言:在C++中,ostringstream、istringstream和stringstream是三个非常有用的字符串流类,它们允许我们以流的方式处理字符串数据。本文将深入探讨这三个类的用法和特性,帮助读者更好地理解和应用字符串流操作。1.ostringstream(输出字符串流)ostringstream是C++中用于输出字......