当前位置:网站首页>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
边栏推荐
- 在.NE6 WebApi中使用分布式缓存Redis
- C# 11 的这个新特性,我愿称之最强!
- Maui initial experience: Cool
- Swap the left and right of each node in a binary tree
- MYSQL05_ Ordr by sorting, limit grouping, group by grouping
- C read / write binary file
- Blazor University (11)组件 — 替换子组件的属性
- 最通俗易懂的依赖注入之服务容器与作用域
- 一套关于 内存对齐 的C#面试题,做错的人很多!
- Blazor University (12)组件 — 组件生命周期
猜你喜欢

Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel

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

Use of slice grammar sugar in C #

Small companies don't make formal offers

be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?

Introduction and use of openfeign component

2022山东省安全员C证上岗证题库及在线模拟考试

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

Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
![FileNotFoundError: [Errno 2] No such file or directory](/img/ea/0c3f2768d14c1f4bb42bd1309ab996.png)
FileNotFoundError: [Errno 2] No such file or directory
随机推荐
Load view Caton
荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
建立与遍历二叉树
Blazor University (12)组件 — 组件生命周期
C syntax sugar empty merge operator [?] And null merge assignment operator [? =]
Judge whether there is a leap year in the given year
Dynamic sequence table + OJ
MySQL port is occupied when building xampp
Blazor University (11)组件 — 替换子组件的属性
最通俗易懂的依赖注入与控制反转
基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
Vs code setting line feed
[new version release] componentone added Net 6 and blazor platform control support
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
在.NE6 WebApi中使用分布式缓存Redis
Tips in MATLAB
ASP.NET 6 中间件系列 - 条件中间件
C# 读写二进制文件
Two methods are used to solve the "maximum palindrome product" problem
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解