1. Overview
1.概述
In this article, we’ll learn about breaking YAML strings over multiple lines.
在这篇文章中,我们将学习如何在多行中分解YAML字符串。
In order to parse and test our YAML files, we’ll make use of the SnakeYAML library.
为了解析和测试我们的YAML文件,我们将利用SnakeYAML库。
2. Multi-Line Strings
2. 多行字符串
Before we begin, let’s create a method to simply read a YAML key from a file into a String:
在我们开始之前,让我们创建一个方法来简单地从文件中读取YAML键到String。
String parseYamlKey(String fileName, String key) {
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream(fileName);
Map<String, String> parsed = yaml.load(inputStream);
return parsed.get(key);
}
In the next subsections, we’ll look over a few strategies for splitting strings over multiple lines.
在接下来的几个小节中,我们将探讨在多行上分割字符串的一些策略。
We’ll also learn how YAML handles leading and ending line breaks represented by empty lines at the beginning and end of a block.
我们还将学习YAML如何处理由块的开头和结尾的空行所代表的前导和结束的换行。
3. Literal Style
3.文字风格
The literal operator is represented by the pipe (“|”) symbol. It keeps our line breaks but reduces empty lines at the end of the string down to a single line break.
字面运算符由管道(”|”)符号表示。它保留了我们的换行符,但将字符串末尾的空行减少到一个换行符。
Let’s take a look at the YAML file literal.yaml:
让我们看一下YAML文件literal.yaml。
key: |
Line1
Line2
Line3
We can see that our line breaks are preserved:
我们可以看到,我们的换行符被保留了。
String key = parseYamlKey("literal.yaml", "key");
assertEquals("Line1\nLine2\nLine3", key);
Next, let’s take a look at literal2.yaml, which has some leading and ending line breaks:
接下来,让我们看看literal2.yaml,它有一些前导和结尾的换行:。
key: |
Line1
Line2
Line3
...
We can see that every line break is present except for ending line breaks, which are reduced to one:
我们可以看到,除了结尾的换行符,每一个换行符都是存在的,而结尾的换行符被减少到了一个。
String key = parseYamlKey("literal2.yaml", "key");
assertEquals("\n\nLine1\n\nLine2\n\nLine3\n", key);
Next, we’ll talk about block chomping and how it gives us more control over starting and ending line breaks.
接下来,我们将讨论块状咬合,以及它如何让我们对开始和结束的换行进行更多控制。
We can change the default behavior by using two chomping methods: keep and strip.
我们可以通过使用两种咬合方法来改变默认行为:保持和剥离。
3.1. Keep
3.1. 保持
Keep is represented by “+” as we can see in literal_keep.yaml:
正如我们在literal_keep.yaml中看到的,Keep用 “+”表示。
key: |+
Line1
Line2
Line3
...
By overriding the default behavior, we can see that every ending empty line is kept:
通过覆盖默认行为,我们可以看到每一个结尾的空行都被保留。
String key = parseYamlKey("literal_keep.yaml", "key");
assertEquals("Line1\nLine2\nLine3\n\n", key);
3.2. Strip
3.2 剥离
The strip is represented by “-” as we can see in literal_strip.yaml:
正如我们在literal_strip.yaml中看到的那样,条带用”-“表示。
key: |-
Line1
Line2
Line3
...
As we might’ve expected, this results in removing every ending empty line:
正如我们可能已经预料到的那样,这导致删除了每一个结束的空行。
String key = parseYamlKey("literal_strip.yaml", "key");
assertEquals("Line1\nLine2\nLine3", key);
4. Folded Style
4.折叠式
The folded operator is represented by “>” as we can see in folded.yaml:
正如我们在folded.yaml中看到的,折叠操作符用”>”表示。
key: >
Line1
Line2
Line3
By default, line breaks are replaced by space characters for consecutive non-empty lines:
默认情况下,对于连续的非空行,换行符被替换为空格字符:。
String key = parseYamlKey("folded.yaml", "key");
assertEquals("Line1 Line2 Line3", key);
Let’s look at a similar file, folded2.yaml, which has a few ending empty lines:
让我们看看一个类似的文件,folded2.yaml,它有几个结尾的空行。
key: >
Line1
Line2
Line3
...
We can see that empty lines are preserved, but ending line breaks are also reduced to one:
我们可以看到,空行被保留了,但结尾的换行符也被减少到了一个。
String key = parseYamlKey("folded2.yaml", "key");
assertEquals("Line1 Line2\n\nLine3\n", key);
We should keep in mind that block chomping affects the folding style in the same way it affects the literal style.
我们应该记住,block chomping对折叠风格的影响与对字面风格的影响一样。
5. Quoting
5.引述
Let’s have a quick look at splitting strings with the help of double and single quotes.
让我们快速了解一下在双引号和单引号的帮助下分割字符串的情况。
5.1. Double Quotes
5.1.双重引语
With double quotes, we can easily create multi-line strings by using “\n“:
有了双引号,我们可以通过使用”\n“轻松创建多行字符串。
key: "Line1\nLine2\nLine3"
String key = parseYamlKey("plain_double_quotes.yaml", "key");
assertEquals("Line1\nLine2\nLine3", key);
5.2. Single Quotes
5.2.单一报价
On the other hand, single-quoting treats “\n” as part of the string, so the only way to insert a line break is by using an empty line:
另一方面,单引号将”\n“视为字符串的一部分,所以插入换行的唯一方法是使用一个空行。
key: 'Line1\nLine2
Line3'
String key = parseYamlKey("plain_single_quotes.yaml", "key");
assertEquals("Line1\\nLine2\nLine3", key);
6. Conclusion
6.结语
In this quick tutorial, we’ve looked over multiple ways of breaking YAML strings over multiple lines through quick and practical examples.
在这个快速教程中,我们通过快速而实用的例子,了解了在多行上分解YAML字符串的多种方法。
As always, the code is available over on GitHub.
像往常一样,代码可在GitHub上获得。