String 如何在YAML中多行断开字符串?
在YAML中,我有一根非常长的绳子。我想将其保留在编辑器的80列(或更多)视图中,因此我想中断字符串。这个的语法是什么 换句话说,我有:String 如何在YAML中多行断开字符串?,string,syntax,yaml,newline,String,Syntax,Yaml,Newline,在YAML中,我有一根非常长的绳子。我想将其保留在编辑器的80列(或更多)视图中,因此我想中断字符串。这个的语法是什么 换句话说,我有: Key: 'this is my very very very very very very long string' 我想要这个(或者类似的东西): 我希望如上所述使用引号,因此不需要转义字符串中的任何内容。使用yaml折叠样式。每行中的缩进将被忽略。将在末尾插入换行符 Key: > This is a very long sentence
Key: 'this is my very very very very very very long string'
我想要这个(或者类似的东西):
我希望如上所述使用引号,因此不需要转义字符串中的任何内容。使用yaml折叠样式。每行中的缩进将被忽略。将在末尾插入换行符
Key: >
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with only a single carriage return appended to the end.
您可以使用“block chomping indicator”消除尾随换行符,如下所示:
Key: >-
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with NO carriage returns.
在这两种情况下,每一个换行符都将替换为一个空格
还有其他可用的控制工具(例如,用于控制缩进)
请参见以保留新行使用
|
,例如:
|
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with newlines preserved.
翻译成“这是一个很长的句子\n跨YAML中的多行\n但将作为字符串呈现\n保留换行符。\n“在YAML中写入多行字符串的方法有5种6种(或63种*,具体取决于您的计数方式)
TL;博士
- 大多数情况下都要使用
:内部换行符会被去掉,尽管在末尾会有一个换行符:key: > Your long string here.
Key: | this is my very very very long string
- 如果希望将这些换行符保留为
(例如,嵌入段落标记),请使用\n
|
- 如果不希望在末尾附加换行符,请使用
或-
|-
Key: >
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with only a single carriage return appended to the end.
key: "Antidisestab\
lishmentarianism.\n\nGet on it."
,|
)
它们允许诸如\
和“
等字符不转义,并在字符串末尾添加新行(\n
)
删除字符串中的单个换行符(但在末尾添加一个换行符,并将双换行符转换为单个换行符):
→ 这是我的非常长的字符串\n
|
将字符串中的每个换行符转换为文字换行符,并在末尾添加一个换行符:
key: >
Your long
string here.
Key: |
this is my very very very
long string
→ 这是我的非常长的字符串\n
以下是来自
标量内容可以用块表示法编写,使用文字样式(以“|”表示),其中所有换行符都是有效的。或者,也可以使用折叠样式(以“>”表示)编写标量内容,其中每个换行符都折叠到一个空格,除非它以空行或更缩进的行结尾
带块咀嚼指示器的块样式(-
,-
,+
,++
)
您可以通过添加一个字符来控制字符串中最后一行新行以及任何尾随空行(\n\n
)的处理:
Key: this is my very very very
long string
,
:“剪辑”:保留换行,删除尾随的空行|
,-
:“条带”:删除换行,删除后面的空行|-
,+
:“保留”:保留换行,保留尾随的空行|+
,“
,”
)
它们具有有限的转义,并且构造一个没有新行字符的单行字符串。它们可以与键在同一行上开始,也可以先使用其他换行符
(无转义、无#
或:
组合、第一个字符限制):
(\
和“
必须通过\
进行转义,可以使用文字\n
序列插入换行符,可以在不带空格的情况下将行串联起来,并带有尾随的\
):
→ <代码>“这是我的非常非常长的字符串。\n\n爱,亚姆。”
(literal“
必须加倍,没有特殊字符,可能用于表示以双引号开头的字符串):
→ <代码>“这是我的非常非常长的字符串,不是吗。”
总结
在本表中,\uu
表示空格字符
\n
表示“换行符”(\n
在JavaScript中),但“行内换行符”行除外,该行的字面意思是反斜杠和n)
例子
注意“空格”前一行的尾随空格
带有缩进指示器的块样式
如果上面的内容对您来说还不够,您可以添加一个“”(在您的块咀嚼指示器之后,如果您有):
补遗
如果在折叠样式的第一行开始处而不是第一行开始处插入额外的空格,则会保留这些空格,并增加换行符。流动样式不会出现这种情况:
- >
my long
string
- my long
string
→ <代码>[“我的长字符串”\n、“我的长字符串”]
我甚至不能
*
2个块样式,每个块样式有2个可能的块缩进指示器(或无),9个可能的缩进指示器(或无),1个普通样式和2个引用样式:2 x(2+1)x(9+1)+1+2=63
其中一些信息也被总结了。您可能不相信,但YAML也可以使用多行键:
?
>
multi
line
key
:
value
如果您在Symfony中使用YAML和Twig进行翻译,并且希望在Javascript中使用多行翻译,则会在翻译后添加回车符。因此,即使是以下代码:
var javascriptVariable=“{{-'key'| trans-}}”
具有以下yml翻译:
key: >
This is a
multi line
translation.
仍将生成以下html代码:
var javascriptVariable = "This is a multi line translation.
";
因此,细枝中的减号不能解决这个问题。解决方法是在yml中的大于号之后加上这个减号:
key: >-
This is a
multi line
translation.
将产生正确的结果,在细枝中的一行上进行多行翻译:
var javascriptVariable = "This is a multi line translation.";
要连接没有空格的长行,请使用double
?
>
multi
line
key
:
value
key: >
This is a
multi line
translation.
var javascriptVariable = "This is a multi line translation.
";
key: >-
This is a
multi line
translation.
var javascriptVariable = "This is a multi line translation.";
key: "Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemp\
orincididuntutlaboreetdoloremagnaaliqua."
key: "String \
with long c\
ontent"
key: "String\
\ with lon\
g content"
---
# Note: It has 1 new line after the string
content:
Arbitrary free text
over multiple lines stopping
after indentation changes...
...
{
"content": "Arbitrary free text over multiple lines stopping after indentation changes..."
}
---
# After string we have 2 spaces and 2 new lines
content1: |
Arbitrary free text
over "multiple lines" stopping
after indentation changes...
...
{
"content1": "Arbitrary free text\nover \"multiple lines\" stopping\nafter indentation changes... \n"
}
---
# After string we have 2 new lines
plain: |+
This unquoted scalar
spans many lines.
...
{
"plain": "This unquoted scalar\nspans many lines.\n\n\n"
}
---
# After string we have 2 new lines
plain: |-
This unquoted scalar
spans many lines.
...
{
"plain": "This unquoted scalar\nspans many lines."
}
---
folded_newlines: >
this is really a
single line of text
despite appearances
...
{
"fold_newlines": "this is really a single line of text despite appearances\n"
}