A string literal or anonymous string is a literal for a string value in source code. Commonly, a programming language includes a string literal code construct that is a series of characters enclosed in bracket delimiters – usually quote marks. In many languages, the text "foo" is a string literal that encodes the text foo but there are many other variations.
Syntax
Bracket delimited A bracketed string literal is delimited by a start and an end character. The language can specify the use of any characters as delimiters. Quotation is the most common way to delimit a string literal. Many languages support double-quotes (i.e. "Hello") and/or single-quotes (i.e. 'there'). When both are supported, delimiter collision can be minimized by treating one style of quotes as normal text when enclosed in quotes of the other style. In Python the literal "Dwayne 'the rock' Johnson" is valid since the outer quotes are double, making the inner single quotes regular text. An empty string is written as "" or ''. Paired delimiters are two different types of characters where one is used at the beginning of a literal and the other used at the end. With paired delimiters, the language can support embedding quotes in the literal text – as long as they all are paired and don't partially jump out of their own scope. For example, PostScript uses parentheses, as in (The quick (brown fox)) and m4, uses backtick ` at the start, and apostrophe ' at the end. Tcl allows both quotes and braces, as in "The quick brown fox" or {The quick {brown fox}}; this derives from the single quotations in Unix shells and the use of braces in C for compound statements, since blocks of code is in Tcl syntactically the same thing as string literals – that the delimiters are paired is essential for making this feasible. Quotation is most commonly via unpaired quotes, but some tools and character sets support paired quotes. Unpaired quotes are quotes that don't have unique left-side-quote and right-side-quote variants, including "" or ''. The Unicode character set includes paired versions:
“Hi there!” ‘Hi there!’ „Hi there!“ «Hi there!»
Scope Mixing Scope mixing is where scopes are mixed together instead of being purely hierarchical.
In other words, "scope mixing" is where an unambiguous beginning delimiter (or an unambiguous ending delimiter) doesn't exist. Examples of a quote jumping out of its own scope (which are illegal strings) would be:
"InsideTheOuterString 'InnerString_ButWithUnmatchedEndingSingleQuoteThatRendersThisStringInvalid" ErroneousStringThatIsSomehowInsideTheInnerStringYetNotInsideTheOuterString' "1 '2" 3' "Inside1 'InsideBoth1and2" Inside2' String literals do not allow scope mixing, as it creates ambiguous programs. A string literal may sometimes appear to have mixed scopes, but there are operator-precedence rules
made for each specific programming language to prevent ambiguity, such as the tactic of reading expressions from the leftmost character to the rightmost character, giving a left " symbol a higher precedence (i.e., priority) over any other quotation mark that comes after it (on the same line). As mentioned earlier, nested quotes can be valid, such as in "Dwayne 'the rock' Johnson", but they require some method that employs a hierarchy (typically a stack data structure), regardless of whether that hierarchy uses unique delimiting characters, escape characters, or repeated-character delimiters. "Scope-mixing" is a specific case of Delimiter Collision, explained later in this page.
Whitespace delimited A language might support multi-line strings. In YAML, string literals may be specified by the relative positioning of whitespace and indentation.
Word delimited Some languages, such as Perl and PHP, allow string literals that are delimited the same as words in a natural language. In the following Perl code, for example, red, green, and blue are string literals, even though not quoted:
Perl treats a non-reserved sequence of alphanumeric characters as string literal in most contexts. For example, the following two lines of Perl are equivalent:
Declarative notation The length of a literal can be encoded into the beginning of the text which alleviates the need for marking the beginning and end of a string. For example, in FORTRAN, string literals were written in Hollerith notation, where a decimal count of the number of characters was followed by the letter H, and then the characters of the string.
A drawback of this technique is that it is relatively error-prone unless length insertion is automated, especially for multi-byte encodings. Advantages include: alleviates need to search for the end delimiter and therefore requires less computational overhead, prevents delimiter collision issues and enables the inclusion of metacharacters that might otherwise be mistaken as commands
Delimiter collision
When using quoting, if one wishes to represent the delimiter itself in a string literal, one runs into the problem of delimiter collision. For example, if the delimiter is a double quote, one cannot simply represent a double quote itself by the literal """ as the second quote is interpreted as the end of the string literal, not as the value of the string, and similarly one cannot write "This is "in quotes", but invalid." as the middle quoted portion is instead interpreted as outside of quotes. There are various solutions, the most general-purpose of which is using escape sequences, such as "\"" or "This is \"in quotes\" and properly escaped.", but there are many other solutions. Paired quotes, such as braces in Tcl, allow nested strings, such as {foo {bar} zork} but do not otherwise solve the problem of delimiter collision, since an unbalanced closing delimiter cannot simply be included, as in {}}.
Doubling up A number of languages, including Pascal, BASIC, DCL, Smalltalk, SQL, J, and Fortran, avoid delimiter collision by doubling up on the quotation marks that are intended to be part of the string literal itself:
Dual quoting Some languages, such as Fortran, Modula-2, JavaScript, Python, and PHP allow more than one quoting delimiter; in the case of two possible delimiters, this is known as dual quoting. Typically, this consists of allowing the programmer to use either single quotations or double quotations interchangeably – each literal must use one or the other.
This does not allow having a single literal with both delimiters in it, however. This can be worked around by using several literals and using string concatenation:
… excerpt ends here. Continue reading the full article.