Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
String literal
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Multiline string literals== In many languages, string literals can contain literal newlines, spanning several lines. Alternatively, newlines can be escaped, most often as <code>\n</code>. For example: <syntaxhighlight lang="bash"> echo 'foo bar' </syntaxhighlight> and <syntaxhighlight lang="bash"> echo -e "foo\nbar" </syntaxhighlight> are both valid bash, producing: foo bar Languages that allow literal newlines include bash, Lua, Perl, PHP, R, and Tcl. In some other languages string literals cannot include newlines. Two issues with multiline string literals are leading and trailing newlines, and indentation. If the initial or final delimiters are on separate lines, there are extra newlines, while if they are not, the delimiter makes the string harder to read, particularly for the first line, which is often indented differently from the rest. Further, the literal must be unindented, as leading whitespace is preserved β this breaks the flow of the code if the literal occurs within indented code. The most common solution for these problems is [[here document]]-style string literals. Formally speaking, a [[here document]] is not a string literal, but instead a stream literal or file literal. These originate in shell scripts and allow a literal to be fed as input to an external command. The opening delimiter is <code><<END</code> where <code>END</code> can be any word, and the closing delimiter is <code>END</code> on a line by itself, serving as a content boundary β the <code><<</code> is due to redirecting [[stdin]] from the literal. Due to the delimiter being arbitrary, these also avoid the problem of delimiter collision. These also allow initial tabs to be stripped via the variant syntax <code><<-END</code> though leading spaces are not stripped. The same syntax has since been adopted for multiline string literals in a number of languages, most notably Perl, and are also referred to as ''here documents,'' and retain the syntax, despite being strings and not involving redirection. As with other string literals, these can sometimes have different behavior specified, such as variable interpolation. Python, whose usual string literals do not allow literal newlines, instead has a special form of string, designed for multiline literals, called ''triple quoting''. These use a tripled delimiter, either <code><nowiki>'''</nowiki></code> or <code>"""</code>. These literals are especially used for inline documentation, known as [[docstring]]s. Tcl allows literal newlines in strings and has no special syntax to assist with multiline strings, though delimiters can be placed on lines by themselves and leading and trailing newlines stripped via <code>string trim</code>, while <code>string map</code> can be used to strip indentation.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)