当前位置:网站首页>This new feature of C 11, I would like to call it the strongest!
This new feature of C 11, I would like to call it the strongest!
2022-04-23 03:08:00 【Dotnet cross platform】
Preface
In daily development, we often use JSON、XML、HTML、SQL、Regex Wait for the string to copy and paste into our code , These strings often contain a lot of quotation marks ", We have to add escape characters to all quotation marks one by one \ Transference . This is very troublesome , And easy to make mistakes , And when we need to replace these strings , The re pasted text still needs to be escaped again , It's just breaking down .
In my work, I often encounter such a scene , For a core business approach , Before business execution, log output parameter objects are generally used (Json Format ), In this way, the parameter value at that time can be traced in case of business failure , It is conducive to afterwards compensation or recurrence bug etc. .
Suppose a fault occurs , I got the parameter value from the log. The content is as follows
{
"kpzdbs": "YJ2",
"jqbh": "499098528334",
"fplxdm": "026",
"fpqqlsh": "ZJBX965525",
"kplx": 0,
"tspz": "00"
}
To reproduce the problem , In console or unit tests , Define this parameter , And call MyLogic Method ,
You can see that the error string is copied directly from the log json Pasted in , Need for all " Transference , This is a very painful problem , Sometimes you may need to paste many objects in the log to test , Insert escape symbols after pasting each time

Now we don't need to do this .
Original string of new features
Original string text Is a new format for string text . The original string text can contain any text , Including Spaces 、 New line 、 Embed quotation marks and other special characters , Without an escape sequence . The original string text starts with at least three double quotes , (""") Characters . It ends with the same number of double quote characters . Usually , The original string text uses three double quotes on a single line to start the string , Three double quotes on the other line end the string . The closing quotation mark and the line break before the closing quotation mark are not included in the final content . Refer to here for details
Use cases
I use this new feature to modify the code in the above scenario as follows , You can see ,json The string can be pasted directly without any escape .

Another example is that I need to paste a paragraph html, Without this new feature , Escape so many quotes , You may be crazy !!
The use of the original string should pay attention to
Use at least three double quotes (
"""...""") The beginning and the end , Any number of double quotation marks less than the number of open original string text is considered content .The closing quotation mark and the line break before the closing quotation mark are not included in the final content .
The original string text usually uses three double quotes on a single line to start the string , Three double quotes on the other line end the string .
You can talk to
$( There can be multiple ) Use with interpolated strings , Prefix$The number of is the number of curly braces required to indicate nested code expressions . If the original string text starts with$$The prefix , A single brace is treated as the content , And you need two curly braces to indicate nested code . It's like using quotation marks , You can add more$To allow more braces to be treated as content .
When more than three double quotes are needed
When the content of the string contains at least three " Come together , Your continuous presence " Add the number of 1( At least three ), Namely Original string Use... At the beginning and end " The number of , So it's not fixed three "
Be careful : Before the string in the figure | Not the character you entered , Is the alignment prompt of the compiler .
Output 
The closing quotation mark and the line break before the closing quotation mark are not included in the final content
As follows , The compiler automatically ends with """" The front shows a vertical line for the boundary , The space to the left of the vertical line is not counted as content .
The three quotation marks at the end can't start another line
Put the ending character after the content , The compiler will prompt you to put it on your own line 
This can be interpreted as , In this case, it may cause ambiguity , The compiler is not sure which of the following three is your original string .
you """ good """ ah
or ( There's a space in front )
you """ good """ ah
or ( Line break + There's a space in front )
you """ good """ ah
If so , There will be no ambiguity , Bounded by the vertical line pointed by the arrow , The space on the left does not count as content , The space on the right is counted as the content

If it's all in one line , Because there will be no ambiguity .

Use with interpolated characters
This is more difficult to understand ,$ The number of symbols is also not fixed , Depending on how many curly braces you need to use in succession as the original character
When the content you want to output has no parentheses , It belongs to the general usage , There's nothing to say .
If you want to output the following
{ Zhang San }
Tips for meeting this problem 
The simple understanding is , You need to treat a brace as content , Two are needed $ As a prefix , You need to n Parentheses are for the content , Need n + 1 individual $ Prefix , Similarly, the interpolation of strings also requires the consumption of $ The same number of braces .
Because you want to treat a brace as content , So we need to 1 + 1 = 2 individual $, Again , Interpolated variables also need to follow $ A consistent number of parentheses wrap , So the modified code is as follows .
If what you want to output is
{
{
{
{ Zhang San
You need to treat the four parentheses as content ,n = 4, $ The number of 4 + 1 = 5, Interpolated variables also need 5 And bracket wrapping .

summary
When we paste quoted text into the compiler, we need to do a lot of escape operations , Original string The feature can solve this problem well , Let's have fun pasting these strings , But when used with interpolated strings , In particular, multiple brace scenes are still difficult to understand , Fortunately, the compiler will prompt how to do , And those scenes are extremely uncommon .
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301333139.html
边栏推荐
- 类似Jira的十大项目管理软件
- 2022山东省安全员C证上岗证题库及在线模拟考试
- [software testing] understand the basic knowledge of software testing
- 微软是如何解决 PC 端程序多开问题的——内部实现
- Creating wechat voucher process with PHP
- 樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
- Simple example of using redis in PHP
- [format] simple output (2)
- MySQL port is occupied when building xampp
- 7-11 rearrange the linked list (25 points)
猜你喜欢

Detailed log display of openfeign call

How does Microsoft solve the problem of multiple programs on PC side -- internal implementation

Response processing of openfeign

由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。

2022年度Top9的任务管理系统

Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)

樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統

AspNetCore配置多环境log4net配置文件

Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!

Source generator actual combat
随机推荐
TP5 where query one field is not equal to multiple values
Openfeign service call
Passing object type parameters through openfeign
C语言实现通讯录----(静态版本)
2022T电梯修理考试模拟100题及在线模拟考试
[format] simple output (2)
Using positive and negative traversal to solve the problem of "the shortest distance of characters"
Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
C# WPF UI框架MahApps切换主题
.NET点滴:说说Middleware构造中获取不到Scoped服务的问题
微软是如何解决 PC 端程序多开问题的
Er and eer models
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
Tencent video price rise: earn more than 7.4 billion a year! Pay attention to me to receive Tencent VIP members, and the weekly card is as low as 7 yuan
TP5 inherits base and uses the variables in base
OLED多级菜单记录
Laravel's own paging query
2022山东省安全员C证上岗证题库及在线模拟考试
Small companies don't make formal offers
Summary of interface automation interview questions for software testing