公告

大中华汽车电子生态圈社区并入开发者社区- 更多资讯点击此

Tip / 登入 to post questions, reply, level up, and achieve exciting badges. Know more

cross mob

XMC实验分享之116: C++ raw string

XMC实验分享之116: C++ raw string

User12775
Level 5
Level 5
First solution authored First like received

C语言表达字符串时很多字符都需要转义符, 比如:

const char* s2 = "
Hello
World
";

打印出来就是

Hello
World

如果想打印 Hello World

就必须这么写:

const char* s2 = "\nHello\nWorld\n";

如果字符串比较短, 手工添加转义符工作量不大, 但是如果字符串比较长, 手工添加转义符就很繁重, 尤其是网络编程, 经常要发送大段的字符串, 比如:

<html>

<head>
<title>XMC4700/XMC4800 RelaxKit Web Server</title>
</head>

<body>

<script>
function change1(){ 
top.frames.ifr.location.href="./led1.html?LED1=OnOff"; 
} 

function change3(){ 
top.frames.ifr.location.href="./dts.html?DTS=Console"; 
} 
</script>

<div align=center>
<p font=16>XMC4700 RelxKit Web Server Demo</p>
<p>Press button to control board</p>
<p/><a href="404.htm"><img src="img_sis.gif"></img></a>

<button type="button" onclick="change1()">Switch LED1 </button>
<button type="button" onclick="change3()">Die Temperature</button>
<iframe id="ifr" name="ifr" frameborder="0" scrolling="no" width="0" height="0"></iframe> 
</div>

</body>

</html>

如果要添加手工添加转义符, 工作量很大. 这时候如果用C++的raw string可以大大简化代码工作, 注意, 这个特性需要C++11之后的标准才支持.

0 点赞
862 次查看
3 评论