有没有一种方法可以表示 YAML 文档中多行中没有任何空格的长字符串?
假设我有以下字符串:
"abcdefghijklmnopqrstuvwxyz"
并且我认为对于 YAML 文件中的一行来说它太长了,是否有某种方法可以将其拆分为几行?
>-
abcdefghi
jklmnopqr
stuvwxyz
会导致 "abcdefghi jklmnopqr stuvwxyz"
很接近,但不应该有任何空格。
Say I have the following string:
"abcdefghijklmnopqrstuvwxyz"
And I think its too long for one line in my YAML file, is there some way to split that over several lines?
>-
abcdefghi
jklmnopqr
stuvwxyz
Would result in "abcdefghi jklmnopqr stuvwxyz"
which is close, but it shouldn't have any spaces.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用双引号,并转义换行符:
Use double-quotes, and escape the newline:
杰西的回答可能会忽略一些微妙之处。
YAML(像许多编程语言一样)以不同方式对待单引号和双引号。考虑这个文档:
这将无法解析并出现错误,例如:
将其与:
哪个将正确解析。为了在双引号字符串中使用反斜杠字符,您需要对它们进行转义,如下所示:
我还想强调 Steve 对单引号字符串的评论。考虑这个文档:
解析时,你会发现它相当于:
这是 YAML 将单引号字符串视为文字的直接结果,而双引号字符串则受到转义字符扩展的影响。
There are some subtleties that Jesse's answer will miss.
YAML (like many programming languages) treats single and double quotes differently. Consider this document:
This will fail to parse with an error such as:
Compare that to:
Which will parse correctly. In order to use backslash character inside double-quoted strings you would need to escape them, as in:
I'd also like to highlight Steve's comment about single-quoted strings. Consider this document:
When parsed, you will find that it is equivalent to:
This is a direct result of the fact that YAML treats single-quoted strings as literals, while double-quoted strings are subject to escape character expansion.