前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++中使用R“()“标记符书写多行字符串

C++中使用R“()“标记符书写多行字符串

作者头像
ccf19881030
发布2023-09-09 14:37:54
2140
发布2023-09-09 14:37:54
举报
文章被收录于专栏:ccf19881030的博客ccf19881030的博客

在C#中使用@表示的字符串能够跨越数行。用于在C#中写JS或SQL代码比较方便。

代码语言:javascript
复制
string sqlInsert = @"INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 1, 'a04005', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 2, 'a04006', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 3, 'a04007', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 1, 'a99501', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 2, 'a99502', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 3, 'a99500', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 4, 'a99505', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 5, 'a99504', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 6, 'a99503', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 23, 'a24901', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 27, 'a24904', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 28, 'a24905', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 29, 'a24042', '.3');
                                INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 36, 'a25002', '.3');)";

Console.WriteLine(sqlInsert);

运行结果如下图所示:

CSharp
CSharp
代码语言:javascript
复制
string s_JavaScript = @"
            <script type=""type/javascript"">
            function doSomething()
            {
            }
            </script>";

那么在C++中有没有比较方便的方式书写SQL脚本呢?因为在实际编程中,对于那种较长的SQL脚本,我们如果在代码中一行写的话有时不容易阅读和理解。在油管上看到C++博主The Cherno的一篇String Literals in C++的视频,里面提到了使用R"()"标记符书写多行字符串的用法。

原始的C/C++语言可以按照下面那样书写多行的字符串

代码语言:javascript
复制
const char* name005 = "line1\n"
		"line2\n"
		"line3\n";

不过庆幸的是C++中提供了R"()"的方式书写多行字符串,如下所示:

代码语言:javascript
复制
#include <iostream>
#include <string>

int main()
{
   std::string sqlInsert = R"(INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 1, 'a04005', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 2, 'a04006', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 3, 'a04007', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 1, 'a99501', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 2, 'a99502', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 3, 'a99500', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 4, 'a99505', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 5, 'a99504', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 6, 'a99503', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 23, 'a24901', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 27, 'a24904', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 28, 'a24905', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 29, 'a24042', '.3');
							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 36, 'a25002', '.3');)";

	std::cout << sqlInsert << std::endl;

	return 0;
}

运行结果如下图所示:

C++ R标记符
C++ R标记符
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-09-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档