string 如何在多行上断开一个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3790454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I break a string over multiple lines?
提问by jjkparker
In YAML, I have a string that's very long. I want to keep this within the 80-column (or so) view of my editor, so I'd like to break the string. What's the syntax for this?
在 YAML 中,我有一个很长的字符串。我想把它保留在我的编辑器的 80 列(左右)视图中,所以我想打破字符串。这是什么语法?
In other words, I have this:
换句话说,我有这个:
Key: 'this is my very very very very very very long string'
and I'd like to have this (or something to this effect):
我想要这个(或类似的东西):
Key: 'this is my very very very ' +
'long string'
I'd like to use quotes as above, so I don't need to escape anything within the string.
我想使用上面的引号,所以我不需要转义字符串中的任何内容。
采纳答案by Matt Williamson
Using yaml folded style, each line break is replaced by a space. The indention in each line will be ignored. A line break will be inserted at the end.
使用 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.
http://symfony.com/doc/current/components/yaml/yaml_format.html
http://symfony.com/doc/current/components/yaml/yaml_format.html
You can use the "block chomping indicator" to eliminate the trailing line break, as follows:
您可以使用“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.
There are other control tools available as well (for controlling indentation for example).
还有其他可用的控制工具(例如用于控制缩进)。
回答by Steve Bennett
There are 56NINE(or 63*, depending how you count) different ways to write multi-line strings in YAML.
在 YAML 中编写多行字符串有5 6 NINE(或 63*,取决于您如何计算)不同的方法。
TL;DR
TL; 博士
Usually, you want
>
:key: > Your long string here.
If you want the linebreaks to be preserved as
\n
in the string (for instance, embedded markdown with paragraphs), use|
.key: | ### Heading * Bullet * Points
Use
>-
or|-
instead if you don't want a linebreak appended at the end.If you need to split lines in the middle of words or literally type linebreaks as
\n
, use double quotes instead:key: "Antidisestab\ lishmentarianism.\n\nGet on it."
YAML is crazy.
通常,你想要
>
:key: > Your long string here.
如果您希望将换行符保留
\n
在字符串中(例如,带有段落的嵌入降价),请使用|
.key: | ### Heading * Bullet * Points
如果您不想在末尾附加换行符,请使用
>-
或|-
代替。如果您需要在单词中间拆分行或字面上将换行符键入为
\n
,请改用双引号:key: "Antidisestab\ lishmentarianism.\n\nGet on it."
YAML 很疯狂。
Block scalar styles (>
, |
)
块标量样式 ( >
, |
)
These allow characters such as \
and "
without escaping, and add a new line (\n
) to the end of your string.
这些允许字符转义\
和"
不转义,并\n
在字符串的末尾添加一个新行 ( )。
>
Folded styleremoves single newlines within the string (but adds one at the end, and converts double newlines to singles):
>
折叠样式删除字符串中的单个换行符(但在末尾添加一个,并将双换行符转换为单行):
Key: >
this is my very very very
long string
→ this is my very very very long string\n
→ this is my very very very long string\n
|
Literal styleturns every newline within the string into a literal newline, and adds one at the end:
|
文字样式将字符串中的每个换行符都转换为文字换行符,并在末尾添加一个:
Key: |
this is my very very very
long string
→ this is my very very very\nlong string\n
→ this is my very very very\nlong string\n
Here's the official definition from the YAML Spec 1.2
这是YAML Spec 1.2的官方定义
Scalar content can be written in block notation, using a literal style (indicated by “|”) where all line breaks are significant. Alternatively, they can be written with the folded style (denoted by “>”) where each line break is folded to a space unless it ends an empty or a more-indented line.
标量内容可以用块表示法编写,使用文字样式(用“|”表示),其中所有换行符都很重要。或者,它们可以用折叠样式(用“>”表示)书写,其中每个换行符都折叠成一个空格,除非它以空行或缩进更多的行结束。
Block styles with block chomping indicator (>-
, |-
, >+
, |+
)
块样式与块咀嚼指示器 ( >-
, |-
, >+
, |+
)
You can control the handling of the final new line in the string, and any trailing blank lines (\n\n
) by adding a block chomping indicatorcharacter:
您可以\n\n
通过添加块压缩指示符来控制对字符串中最后一个新行和任何尾随空行 ( ) 的处理:
>
,|
: "clip": keep the line feed, remove the trailing blank lines.>-
,|-
: "strip": remove the line feed, remove the trailing blank lines.>+
,|+
: "keep": keep the line feed, keep trailing blank lines.
>
,|
: "clip": 保持换行,去掉尾随的空行。>-
,|-
: "strip": 去除换行符,去除尾随空行。>+
,|+
: "keep": 保持换行,保持尾随空行。
"Flow" scalar styles (
, "
, '
)
“流”标量样式 (
, "
, '
)
These have limited escaping, and construct a single-line string with no new line characters. They can begin on the same line as the key, or with additional newlines first.
这些具有有限的转义,并构造一个没有换行符的单行字符串。它们可以与键在同一行开始,或者首先添加额外的换行符。
plain style(no escaping, no #
or :
combinations, limits on first character):
普通样式(无转义,无#
或:
组合,限制第一个字符):
Key: this is my very very very
long string
double-quoted style(\
and "
must be escaped by \
, newlines can be inserted with a literal \n
sequence, lines can be concatenated without spaces with trailing \
):
双引号样式(\
并且"
必须由 转义\
,换行符可以插入一个文字\n
序列,行可以连接而不带尾随空格\
):
Key: "this is my very very \"very\" loooo\
ng string.\n\nLove, YAML."
→ "this is my very very \"very\" loooong string.\n\nLove, YAML."
→ "this is my very very \"very\" loooong string.\n\nLove, YAML."
single-quoted style(literal '
must be doubled, no special characters, possibly useful for expressing strings starting with double quotes):
单引号样式(文字'
必须加倍,没有特殊字符,可能用于表示以双引号开头的字符串):
Key: 'this is my very very "very"
long string, isn''t it.'
→ "this is my very very \"very\" long string, isn't it."
→ "this is my very very \"very\" long string, isn't it."
Summary
概括
In this table, _
means space character
. \n
means "newline character" (\n
in JavaScript), except for the "in-line newlines" row, where it means literally a backslash and an n).
在该表中,_
表示space character
。\n
表示“换行符”(\n
在 JavaScript 中),“内联换行符”行除外,它的字面意思是反斜杠和 n)。
> | " ' >- >+ |- |+
-------------------------|------|-----|-----|-----|------|------|------|------
Trailing spaces | Kept | Kept | | | | Kept | Kept | Kept | Kept
Single newline => | _ | \n | _ | _ | _ | _ | _ | \n | \n
Double newline => | \n | \n\n | \n | \n | \n | \n | \n | \n\n | \n\n
Final newline => | \n | \n | | | | | \n | | \n
Final dbl nl's => | | | | | | | Kept | | Kept
In-line newlines | No | No | No | \n | No | No | No | No | No
Spaceless newlines| No | No | No | \ | No | No | No | No | No
Single quote | ' | ' | ' | ' | '' | ' | ' | ' | '
Double quote | " | " | " | \" | " | " | " | " | "
Backslash | \ | \ | \ | \ | \ | \ | \ | \ | \
" #", ": " | Ok | Ok | No | Ok | Ok | Ok | Ok | Ok | Ok
Can start on same | No | No | Yes | Yes | Yes | No | No | No | No
line as key |
Examples
例子
Note the trailing spaces on the line before "spaces."
请注意“空格”之前行中的尾随空格。
- >
very "long"
'string' with
paragraph gap, \n and
spaces.
- |
very "long"
'string' with
paragraph gap, \n and
spaces.
- very "long"
'string' with
paragraph gap, \n and
spaces.
- "very \"long\"
'string' with
paragraph gap, \n and
s\
p\
a\
c\
e\
s."
- 'very "long"
''string'' with
paragraph gap, \n and
spaces.'
- >-
very "long"
'string' with
paragraph gap, \n and
spaces.
[
"very \"long\" 'string' with\nparagraph gap, \n and spaces.\n",
"very \"long\"\n'string' with\n\nparagraph gap, \n and \nspaces.\n",
"very \"long\" 'string' with\nparagraph gap, \n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \n and spaces."
]
Block styles with indentation indicators
带有缩进指示符的块样式
Just in case the above isn't enough for you, you can add a "block indentation indicator" (after your block chomping indicator, if you have one):
以防万一以上对你来说还不够,你可以添加一个“块缩进指示器”(在你的块咀嚼指示器之后,如果你有的话):
- >8
My long string
starts over here
- |+1
This one
starts here
Addendum
附录
If you insert extra spaces at the start of not-the-first lines in Folded style, they will be kept, with a bonus newline. This doesn't happen with flow styles:
如果您在折叠样式的非第一行的开头插入额外的空格,它们将被保留,并带有额外的换行符。流样式不会发生这种情况:
- >
my long
string
- my long
string
→ ["my long\n string\n", "my long string"]
→ ["my long\n string\n", "my long string"]
I can't even.
我什至不能。
*
2 block styles, each with 2 possible block chomping indicators (or none), and with 9 possible indentation indicators (or none), 1 plain style and 2 quoted styles: 2 x (2 + 1) x (9 + 1) + 1 + 2 = 63
*
2 种块样式,每种样式都有 2 个可能的块压缩指示符(或无),以及 9 个可能的缩进指示符(或无)、1 种简单样式和 2 种引用样式:2 x (2 + 1) x (9 + 1) + 1 + 2 = 63
Some of this information has also been summarised here.
回答by Ali Shakiba
To preserve newlinesuse |
, for example:
要保留换行符,请使用|
,例如:
|
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with newlines preserved.
is translated to "This is a very long sentence?\nthat spans several lines in the YAML?\nbut which will be rendered as a string?\nwith newlines preserved.\n"
被翻译成“这是一个很长的句子?\n跨越 YAML 中的几行?\n但哪个将呈现为字符串?\n保留换行符。\n”
回答by Arayan Singh
1. Block Notation(plain, flow-style, scalar):Newlines become spaces and extra newlines after the block are removed
1. Block Notation(plain, flow-style, scalar):删除块后换行符变成空格和额外的换行符
---
# Note: It has 1 new line after the string
content:
Arbitrary free text
over multiple lines stopping
after indentation changes...
...
Equivalent JSON
等效的 JSON
{
"content": "Arbitrary free text over multiple lines stopping after indentation changes..."
}
2. Literal Block Scalar:A Literal Block Scalar |will include the newlines and any trailing spaces. but removes extra
2. 文字块标量:文字块标量| 将包括换行符和任何尾随空格。但删除了额外的
newlines after the block.
块后的换行符。
---
# After string we have 2 spaces and 2 new lines
content1: |
Arbitrary free text
over "multiple lines" stopping
after indentation changes...
...
Equivalent JSON
等效的 JSON
{
"content1": "Arbitrary free text\nover \"multiple lines\" stopping\nafter indentation changes... \n"
}
3. + indicator with Literal Block Scalar:keep extra newlines after block
3. + 带有文字块标量的指标:在块之后保留额外的换行符
---
# After string we have 2 new lines
plain: |+
This unquoted scalar
spans many lines.
...
Equivalent JSON
等效的 JSON
{
"plain": "This unquoted scalar\nspans many lines.\n\n\n"
}
4. – indicator with Literal Block Scalar:–means that the newline at the end of the string is removed.
4. – 带有文字块标量的指示符:–表示删除字符串末尾的换行符。
---
# After string we have 2 new lines
plain: |-
This unquoted scalar
spans many lines.
...
Equivalent JSON
等效的 JSON
{
"plain": "This unquoted scalar\nspans many lines."
}
5. Folded Block Scalar(>):
5.折叠块标量(>):
will fold newlines to spaces and but removes extra newlines after the block.
将换行符折叠为空格,但会在块之后删除额外的换行符。
---
folded_newlines: >
this is really a
single line of text
despite appearances
...
Equivalent JSON
等效的 JSON
{
"fold_newlines": "this is really a single line of text despite appearances\n"
}
for more you can visit my Blog
更多你可以访问我的博客
回答by phs
To concatenate long lines without whitespace, use double quotes and escape the newlines with backslashes:
要连接没有空格的长行,请使用双引号并使用反斜杠转义换行符:
key: "Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemp\
orincididuntutlaboreetdoloremagnaaliqua."
(Thanks @Tobia)
(感谢@Tobia)
回答by Mohsen
You might not believe it, but YAML can do multi-line keys too:
你可能不相信,但 YAML 也可以做多行键:
?
>
multi
line
key
:
value
回答by Rvanlaak
In case you're using YAML and Twig for translations in Symfony, and want to use multi-line translations in Javascript, a carriage return is added right after the translation. So even the following code:
如果您在 Symfony 中使用 YAML 和 Twig 进行翻译,并希望在 Javascript 中使用多行翻译,则在翻译后立即添加回车符。所以即使是下面的代码:
var javascriptVariable = "{{- 'key'|trans -}}";
var javascriptVariable = "{{- 'key'|trans -}}";
Which has the following yml translation:
其中有以下 yml 翻译:
key: >
This is a
multi line
translation.
Will still result into the following code in html:
仍然会在 html 中产生以下代码:
var javascriptVariable = "This is a multi line translation.
";
So, the minus sign in Twig does not solve this. The solution is to add this minus sign after the greater than sign in yml:
所以,Twig 中的减号不能解决这个问题。解决办法是在yml中的大于号后面加上这个减号:
key: >-
This is a
multi line
translation.
Will have the proper result, multi line translation on one line in Twig:
将有正确的结果,在 Twig 的一行上进行多行翻译:
var javascriptVariable = "This is a multi line translation.";
回答by Joe
For situations were the string might contain spaces or not, I prefer double quotes and line continuation with backslashes:
对于字符串可能包含空格或不包含空格的情况,我更喜欢双引号和带有反斜杠的行继续:
key: "String \
with long c\
ontent"
But note about the pitfall for the case that a continuation line begins with a space, it needs to be escaped (because it will be stripped away elsewhere):
但是请注意延续行以空格开头的情况的陷阱,它需要被转义(因为它会在其他地方被剥离):
key: "String\
\ with lon\
g content"
If the string contains line breaks, this needs to be written in C style \n
.
如果字符串包含换行符,则需要以 C 样式编写\n
。
See also this question.
另请参阅此问题。
回答by Irene
None of the above solutions worked for me, in a YAML file within a Jekyll project. After trying many options, I realized that an HTML injection with <br>
might do as well, since in the end everything is rendered to HTML:
在 Jekyll 项目中的 YAML 文件中,上述解决方案都不适合我。在尝试了许多选项后,我意识到 HTML 注入<br>
也可以,因为最终所有内容都呈现为 HTML:
name: |
In a village of La Mancha <br>
whose name I don't <br>
want to remember.
名字:|
在拉曼查<br>
的一个村庄,我<br>
不想记住他的名字。
At least it works for me. No idea on the problems associated to this approach.
至少它对我有用。不知道与此方法相关的问题。