How do I split a string into multiple lines?

Keywords: Javascript

In YAML, my string is very long.I want to keep it in 80 columns (or similar views) of the editor, so I want to break the string.What is this grammar?

In other words, I have this:

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

I want this (or something that achieves this effect):

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

I want to use the quotes above, so I don't need to escape anything in the string.

#1st floor

To preserve line breaks, use |, 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.

Translated to "This is a very long sentence n that spans several lines in YAML, but will be rendered as a string n with line breaks preserved.\n"

#2nd floor

There are 56 NINE (or 63 *, depending on how you count) ways to write multiline strings in YAML.

TL; DR

  • Typically, you need to >:

    key: > Your long string here.
  • Use | if you want to reserve line breaks in the string as \n (for example, embedded markdown with paragraphs).

    key: | ### Heading * Bullet * Points
  • If you do not want to add a line break at the end, use >-or |-instead.

  • If you need to split a line in the middle of a word or type the line break directly into \n, use double quotation marks instead:

    key: "Antidisestab\\ lishmentarianism.\\n\\nGet on it."
  • YAML is crazy.

Block Scalar Style (>, |)

These allow the use of \ and \characters without escaping, and add a new line (\n) to the end of the string.

> Collapse Style Removes a single line break from the string (but adds one at the end and converts a double line break to a single line):

Key: >
  this is my very very very
  long string

→ this is my very very very long string\\n

| Text Style Convert each line break in the string to a text line break and add one at the end:

Key: |
  this is my very very very 
  long string

→ this is my very very very\\nlong string\\n

This is YAML Spec 1.2 Official Definition

Scalar content can be represented with block symbols using a text style (represented by'|'), where all line breaks are valid.Alternatively, they can be written in a fold style (denoted by'>'), where each line break is collapsed to a space unless it ends with an empty or indented line.

Block style with block marker indicator (>-, |-, >+, |+)

You can add Block chopping indicator To control the last new line in the string and the handling of any trailing blank line (\n\n):

  • >, |: "clip": keep wrapping, delete trailing blank lines.
  • >-, |-: "strip": delete the newline, delete the trailing blank line.
  • >+, |+: "Keep": Keep line breaks, followed by blank lines.

Stream scalar style (,',')

These have limited escape and construct single-line strings without line breaks.They can start on the same line as the key, or they can add other line breaks first.

Normal Style (no escape, no #or: combination, first character limit):

Key: this is my very very very 
  long string

Styles caused by double quotation marks (\ and "must be escaped with \, newline characters can be inserted in the original \n sequence, and rows can be joined without trailing \spaces):

Key: "this is my very very \"very\" loooo\
  ng string.\n\nLove, YAML."

→ "this is my very very \\"very\\" loooong string.\\n\\nLove, YAML."

Single Quote Style (Double quotation marks must be used', special characters cannot be used, and may be useful for strings that begin with double quotation marks):

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

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

abstract

In this table, _denotes space character.\n denotes a "line break" (in JavaScript, \n), except for an "line break", which literally means backslash and 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       |

Example

Notice the trailing space in 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 style with indentation indicator

In case the above does not meet your needs, you can add a " Block indentation indicator (If so, after the block cutting indicator):

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

appendix

If you insert extra spaces in a polyline style at the beginning of a non-first line, those spaces will be preserved with additional line breaks.This does not happen with stream styles:

- >
    my long
      string
- my long
    string

→ ["my long\\n string\\n", "my long string"]

I can't even do it

* 2 block styles, each with 2 possible block indicators (or none), 9 possible indentation indicators (or none), 1 common style, and 2 quoted styles: 2 x (2 + 1) x (9 +1) + 1 + 2 = 63

Some of this information has also been Here A summary is made.

#3rd floor

You may not believe it, but YAML can also perform multiline keys:

?
 >
 multi
 line
 key
:
  value

#4th floor

If you use YAML and Twig for translation in Symfony and want to use multiline translation in Javascript, add carriage return immediately after translation.So even the following code:

var javascriptVariable = "{{- 'key'|trans -}}";

Where yml is translated as follows:

key: >
    This is a
    multi line 
    translation.

This will still result in the following html code:

var javascriptVariable = "This is a multi line translation.
";

Therefore, the minus sign in Twig does not solve this problem.The solution is to add this minus sign after the greater than sign in the yml:

key: >-
    This is a
    multi line 
    translation.

You will get the correct results and translate multiple lines on one line of Twig:

var javascriptVariable = "This is a multi line translation.";

#5th floor

With the yaml collapse style, each line break is replaced by a space.Indentation of each line will be ignored.A line break will be inserted at the end.

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

You can use the Block Record Indicator to eliminate trailing line breaks as follows:

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 (for example, to control indentation).

See https://yaml-multiline.info/

Posted by nfr on Sun, 08 Dec 2019 11:47:09 -0800