在YAML中,我如何分割多行的字符串?

在YAML中,我有一个很长的字符串。 我想保留在我的编辑器的80列(或左右)视图内,所以我想打破这个字符串。 这是什么语法?

换句话说,我有这样的:

Key: 'this is my very very very very very very long string'

我想要这个(或者是这个效果):

Key: 'this is my very very very ' +
     'long string'

我想要使​​用上面的引号,所以我不需要在字符串内跳过任何内容。


使用yaml折叠样式,每个换行符都被空格替换。 每行中的缩进将被忽略。

>
  This is a very long sentence
  that spans several lines in the YAML
  but which will be rendered as a string
  without carriage returns.

http://symfony.com/doc/current/components/yaml/yaml_format.html


在YAML中有5 6个NINE (或63 *,取决于你如何计算)不同的方式来编写多行字符串。

块标量样式( >|

这些允许字符如"而不逃逸,并在字符串的末尾添加一个新行( n )。

>折叠样式删除字符串中的单个换行符(但在末尾添加一个新换行符,并将双换行符转换为单个):

Key: >
  this is my very very very
  long string

this is my very very very long stringn

| 文字样式将字符串中的每一个换行符转换为文字换行符,并在末尾添加一行:

Key: |
  this is my very very very 
  long string

this is my very very verynlong stringn

这是YAML Spec 1.2的官方定义

可以使用文字样式(用“|”表示),其中所有换行符都是重要的,可以以块表示法编写标量内容。 或者,它们可以用折叠样式(用“>”表示)来书写,其中每个换行符都折叠到一个空格,除非它结束了一个空的或更多缩进的行。

用块状块状指示符块( >-|->+|+

您可以通过添加块chomping指示符字符来控制字符串中最后一行的处理以及任何尾随空行( nn ):

  • >| :“剪辑”:保持换行,删除拖尾的空白行。
  • >-|- :“strip”:删除换行符,删除拖尾的空白行。
  • >+|+ :“保持”:保持换行,保留空行。
  • “流”标量样式( "'

    这些限制了转义,并且构造了一个没有换行字符的单行字符串。 它们可以与密钥在同一行开始,也可以先与其他换行符开始。

    普通样式(没有逸出,无#:组合,对第一字符界限):

    Key: this is my very very very 
      long string
    

    双引号风格( "必须通过转义 ,换行可以用文字插入n序列,线可以无空格被级联尾随 ):

    Key: "this is my very very "very" loooo
      ng string.nnLove, YAML."
    

    "this is my very very "very" loooong string.nnLove, YAML."

    单引号样式(文字'必须加倍,没有特殊字符,可能对表示以双引号开头的字符串有用):

    Key: 'this is my very very "very"
      long string, isn''t it.'
    

    "this is my very very "very" long string, isn't it."

    概要

    在这张表中, _表示space charactern表示“换行符”(在JavaScript中为n ),除了“inline newlines”行之外,它的意思是反斜杠和n)。

                          >     |            "     '     >-     >+     |-     |+
    -------------------------|------|-----|-----|-----|------|------|------|------  
    Trailing spaces   | Kept | Kept |     |     |     | Kept | Kept | Kept | Kept
    Single newline => | _    | n   | _   | _   | _   | _    |  _   | n   | n
    Double newline => | n   | nn | n  | n  | n  | n   |  n  | nn | nn
    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       |
    

    例子

    请注意“空格”之前的行尾空格。

    - >
      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' withnparagraph gap, n and         spaces.n", 
      "very "long"n'string' withnnparagraph gap, n and        nspaces.n", 
      "very "long" 'string' withnparagraph gap, n and spaces.", 
      "very "long" 'string' withnparagraph gap, n and spaces.", 
      "very "long" 'string' withnparagraph gap, n and spaces.", 
      "very "long" 'string' withnparagraph gap, n and         spaces."
    ]
    

    用压痕指示器来阻止样式

    为防止上述情况对您不利,您可以添加一个“块缩进指示符”(在您的块区域指示符之后,如果有的话):

    - >8
            My long string
            starts over here
    - |+1
     This one
     starts here
    

    附录

    如果您在折叠样式的第一行中插入额外的空格,它们将被保留,并带有奖励换行符。 流式样不会发生这种情况:

    - >
        my long
          string
    - my long
        string
    

    ["my longn stringn", "my long string"]

    我什至不能。

    * 2个块样式,每个包含2个可能的块嘲讽指示符(或无),以及9个可能的缩进指示符(或无),1个平面样式和2个引用样式:2 x(2 + 1)x(9 + 1)+ 1 + 2 = 63

    这些信息的一部分也在这里总结。


    为了保留换行符,使用| , 例如:

    |
      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并保留新行。”

    链接地址: http://www.djcxy.com/p/20137.html

    上一篇: In YAML, how do I break a string over multiple lines?

    下一篇: How do I comment out a block of tags in XML?