当前位置:网站首页>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
边栏推荐
- Xamarin effect Chapter 21 expandable floating operation button in GIS
- MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
- 利用栈的回溯来解决“文件的最长绝对路径”问题
- 中后二叉建树
- PID debugging of coding motor (speed loop | position loop | follow)
- Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
- How to write the expected salary on your resume to double your salary during the interview?
- ASP.NET和ASP.NETCore多环境配置对比
- Systemctl start Prometheus + grafana environment
- Source Generator实战
猜你喜欢
[new version release] componentone added Net 6 and blazor platform control support
Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!
Realize QQ login with PHP
Blazor University (11)组件 — 替换子组件的属性
MySQL port is occupied when building xampp
Fight leetcode again (290. Word law)
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
PID debugging of coding motor (speed loop | position loop | follow)
【无标题】
2022年度Top9的任务管理系统
随机推荐
Middle and rear binary tree
队列的存储和循环队列
Use DFS to solve the problem of "number of dictionary rows"
Blazor University (12)组件 — 组件生命周期
The most easy to understand dependency injection and control inversion
7-11 rearrange the linked list (25 points)
[ncnn] - the meaning of - 23300 in param
FileNotFoundError: [Errno 2] No such file or directory
What kind of experience is it to prepare for a month to participate in ACM?
Drawing polygons with < polygon / > circular array in SVG tag
Blazor University (11)组件 — 替换子组件的属性
svg标签中利用<polygon/>循环数组绘制多边形
OLED multi-level menu record
Using stack to solve the problem of "mini parser"
Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
Basic SQL (VIII) data update operation practice
【无标题】
C syntax sugar empty merge operator [?] And null merge assignment operator [? =]
腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
Introduction and use of openfeign component