This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
Idiom #48 Multi-line string literal
Assign to variable s a string literal consisting in several lines of text, including newlines.
- Ada
- C
- Clojure
- Cobol
- C++
- C#
- D
- D
- D
- Dart
- Dart
- Elixir
- Elixir
- Elixir
- Erlang
- Fortran
- Go
- Groovy
- Groovy
- Haskell
- Haskell
- JS
- JS
- JS
- Java
- Java
- Java
- Java
- Kotlin
- Lisp
- Lua
- Lua
- Lua
- Obj-C
- PHP
- PHP
- Pascal
- Pascal
- Pascal
- Perl
- Perl
- Perl
- Python
- Python
- Ruby
- Ruby
- Rust
- Rust
- Scala
- Scala
- Scheme
- Smalltalk
- VB
(def s "Murs, ville,
Et port,
Asile
De mort,
Mer grise
Où brise
La brise,
Tout dort.")
Clojure string literals support newlines directly and standard Java string escape sequences
IDENTIFICATION DIVISION.
PROGRAM-ID. multi-line string.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 s PIC X(20).
01 str PIC X(5) VALUE 'COBOL'.
01 str1 PIC X(4) VALUE 'RULE'.
01 str2 PIC X(3) VALUE 'THE'.
01 str3 PIC X(5) VALUE 'WORLD'.
PROCEDURE DIVISION.
STRING str ' ' str1 ' ' str2 ' ' str3
DELIMITED BY SIZE INTO s
STOP RUN.
std::string s = R"( Earth is a planet.
So is the Jupiter)";
Use raw string literals
s = unlines [
"several"
,"lines"
,"of"
,"text"]
unlines concatenates a list of strings, inserting a newline between each.
It is inverse to lines, which splits a string by newlines
It is inverse to lines, which splits a string by newlines
let s = "This is a very long string which needs \
to wrap across multiple lines because \
otherwise my code is unreadable.";
When using backslashes, indentation inside the string literal must be far left.
let s = `This is a very long string which needs
to wrap across multiple lines because
otherwise my code is unreadable.`;
ES6 template literals
let s = "This is a very long string which needs \n" +
"to wrap across multiple lines because \n" +
"otherwise my code is unreadable.";
By concatenation.
String s = "This is a very long string which needs \n" +
"to wrap across multiple lines because \n" +
"otherwise my code is unreadable.";
String s = """
This is a very long string which needs
to wrap across multiple lines because
otherwise my code is unreadable.
""";
Available since Java 15
String s, n = lineSeparator();
s = "line 1" + n;
s = s + "line 2" + n;
s = s + "line 3" + n;
s = s + "line 4";
String s;
StringBuilder b = new StringBuilder();
Formatter f = new Formatter(b);
f.format("line 1%n");
f.format("line 2%n");
f.format("line 3%n");
f.format("line 4");
f.flush();
s = b.toString();
local s = "The quick brown fox \z
jumped over the lazy dog.\
This the second line. "
\ escapes the end of a line. \z *seems* to make it ignore the line end itself as well as any leading spaces. Couldn't find any docs for that, though.
var
s: String;
begin
s := '''
one
two
three
''';
end.
Since Delphi 12 Athens, you can use multilines in pascal code.
var
s: String;
begin
s := 'one' + sLineBreak + 'two' + sLineBreak + 'three'
end.
In Delphi implementation you should use sLineBreak instead of FreePascal's LineEnding constant.
$s =<<EOSTR;
One of the ways to create multiline text is called
the "here doc" (lifted from various UNIX shells).
A 'here doc' is the <<tag construct. Perl continues to treat
all the text found as part of the string until there's a line containing
the EOSTR tag at the beginning
EOSTR
s = ('line 1\n'
'line 2\n'
'line 3\n'
'line 4')
"... String literals that are part of a single expression and have only whitespace between them will be implicitly converted to a single string literal."