Literals/String: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added 11l)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(40 intermediate revisions by 18 users not shown)
Line 17: Line 17:
*   [[Special characters]]
*   [[Special characters]]
*   [[Here document]]
*   [[Here document]]


{{Template:Strings}}
<br><br>
<br><br>


=={{header|11l}}==
=={{header|11l}}==
Character literals:
Character literals:
<lang 11l>V c = Char(‘a’)</lang>
<syntaxhighlight lang="11l">V c = Char(‘a’)</syntaxhighlight>


Regular string literals are enclosed in double quotes, and use \ to delimit special characters:
Regular string literals are enclosed in double quotes, and use \ to delimit special characters:
<lang 11l>"foo\nbar"</lang>
<syntaxhighlight lang="11l">"foo\nbar"</syntaxhighlight>


Raw string literals are enclosed in paired single quotation marks:
Raw string literals are enclosed in paired single quotation marks:
<lang 11l>‘foo
<syntaxhighlight lang="11l">‘foo
bar’</lang>
bar’</syntaxhighlight>
If raw string literal should contains unpaired single quotation marks, then balancing of raw string should be performed:
If raw string literal should contains unpaired single quotation marks, then balancing of raw string should be performed:
<lang 11l>'‘‘don’t’ // the same as "don’t"</lang>
<syntaxhighlight lang="11l">'‘‘don’t’ // the same as "don’t"</syntaxhighlight>
=={{header|6502 Assembly}}==
Strings are enclosed in double quotes.
<syntaxhighlight lang="6502asm">db "Hello World"</syntaxhighlight>
Any typed character in double quotes is assembled as the ASCII equivalent of that character. Therefore the following two data blocks are equivalent:
<syntaxhighlight lang="6502asm">db "Hello World"
db $48,$65,$6c,$6c,$6f,$20,$57,$6f,$72,$6c,$64</syntaxhighlight>

When using a single-character literal as an operand for an instruction, it MUST have a # in front, or else the CPU will treat it as a pointer being dereferenced rather than a numeric constant. (We've all made this mistake at least once without realizing it.)
<syntaxhighlight lang="6502asm">LDA #'A' ;load ascii code of "A" into the accumulator.
LDA 'A' ;load the byte stored at memory address 0x41 into the accumulator.</syntaxhighlight>

The assembler typically assumes nothing with regard to special characters. A <code>\n</code> will be interpreted literally, for example. How special characters are handled depends on the printing routine of the hardware's BIOS, or one created by the programmer. If your printing routine is able to support a null terminator and ASCII control codes, the following represents "Hello World" with the new line command and null terminator:
<syntaxhighlight lang="6502asm">db "Hello World",13,10,0</syntaxhighlight>

Creating your own printing routine is a bit out of the scope of this task but here's a simple demonstration that supports the \n and null termination:
<syntaxhighlight lang="6502asm">PrintString:
lda (StringPtr),y
beq Terminated
cmp #'\' ; a single ascii character is specified in single quotes.
beq HandleSpecialChars
jsr PrintChar ;unimplemented print routine
iny ;next character
jmp PrintString ;back to top

Terminated:
rts ;exit

HandleSpecialChars:
iny ;next char
lda (StringPtr),y
cmp #'n'
beq NextLine ;unimplemented new line routine, it ends in "JMP DoneSpecialChar."
;Typically this would reset the x cursor and increment the y cursor, which are software variables that
;get converted to a VRAM address in some other routine.
DoneSpecialChar:
iny
jmp PrintString ;jump back to top. Notice that neither the backslash nor the character after it were actually printed.</syntaxhighlight>

=={{header|68000 Assembly}}==
{{trans|6502 Assembly}}
Strings are enclosed in double quotes. [[C]] places a null terminator at the end of the string for you; in 68000 Assembly you have to type it manually (unless your assembler has an <code>.ASCIZ</code> directive or equivalent, and not all do).
<syntaxhighlight lang="68000devpac">DC.B "Hello World",0
EVEN</syntaxhighlight>

Any typed character in double quotes is assembled as the ASCII equivalent of that character. Therefore the following two data blocks are equivalent:
<syntaxhighlight lang="68000devpac">DC.B "Hello World",0
EVEN

DC.B $48,$65,$6c,$6c,$6f,$20,$57,$6f,$72,$6c,$64,$00
EVEN</syntaxhighlight>

When using a string literal as an operand for an instruction, it must begin with #, otherwise it will be treated as a pointer being dereferenced rather than the numeric constant you intended.

<syntaxhighlight lang="68000devpac">MOVE.L #'SEGA',D0 ;load the string "SEGA" into D0
MOVE.L '0000',D0 ;load the 32-bit value at address 0x00303030 (the most significant byte is always treated as zero,
;because the 68000 only has a 24-bit address space.</syntaxhighlight>

The assembler typically assumes nothing with regard to special characters. How special characters are handled depends on the printing routine of the hardware's BIOS, or in the case of embedded hardware with no BIOS or a very limited one like the Sega Genesis, the printing routine created by the programmer. By default, there is no support for any control codes unless you add it in yourself.


=={{header|Ada}}==
=={{header|Ada}}==


Single character literals require single quotes
Single character literals require single quotes
<lang Ada>ch : character := 'a';</lang>
<syntaxhighlight lang="ada">ch : character := 'a';</syntaxhighlight>
String literals use double quotes
String literals use double quotes
<lang Ada>msg : string := "hello world";
<syntaxhighlight lang="ada">msg : string := "hello world";
empty : string := ""; -- an empty string</lang>
empty : string := ""; -- an empty string</syntaxhighlight>
The length of a string in Ada is equal to the number of characters in the string.
The length of a string in Ada is equal to the number of characters in the string.
Ada does not employ a terminating null character like C.
Ada does not employ a terminating null character like C.
Line 50: Line 112:
Aime has no character representation, but it allows single quoted character constants. Their implied typed is integer.
Aime has no character representation, but it allows single quoted character constants. Their implied typed is integer.


<lang aime>integer c;
<syntaxhighlight lang="aime">integer c;
c = 'z';</lang>
c = 'z';</syntaxhighlight>


String literals are double quoted.
String literals are double quoted.


<lang aime>text s;
<syntaxhighlight lang="aime">text s;
s = "z";</lang>
s = "z";</syntaxhighlight>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
In ALGOL 68 a single character (CHAR), character arrays ([]CHAR) and strings (STRING) are contained in double quotes. ALGOL 68 also has FORMAT strings which are contained between dollar ($) symbols.
In ALGOL 68 a single character (CHAR), character arrays ([]CHAR) and strings (STRING) are contained in double quotes. ALGOL 68 also has FORMAT strings which are contained between dollar ($) symbols.
<lang algol68>CHAR charx = "z";</lang>
<syntaxhighlight lang="algol68">CHAR charx = "z";</syntaxhighlight>
Strings are contained in double quotes.
Strings are contained in double quotes.
<lang algol68>[]CHAR charxyz = "xyz";
<syntaxhighlight lang="algol68">[]CHAR charxyz = "xyz";
STRING stringxyz = "xyz";
STRING stringxyz = "xyz";
FORMAT twonewlines = $ll$, threenewpages=$ppp$, fourbackspaces=$bbbb$;</lang>
FORMAT twonewlines = $ll$, threenewpages=$ppp$, fourbackspaces=$bbbb$;</syntaxhighlight>
Note: When only uppercase characters sets are available (eg on computers with only
Note: When only uppercase characters sets are available (eg on computers with only
6 bits per "byte") the single quote can used to denote a reserved word. eg
6 bits per "byte") the single quote can used to denote a reserved word. eg
<lang algol68>.PR QUOTE .PR
<syntaxhighlight lang="algol68">.PR QUOTE .PR
[]'CHAR' CHARXYZ = "XYZ";</lang>
[]'CHAR' CHARXYZ = "XYZ";</syntaxhighlight>
The STRING type is simply a FLEX array of CHAR.
The STRING type is simply a FLEX array of CHAR.
<lang algol68>MODE STRING = FLEX[1:0]CHAR;</lang>
<syntaxhighlight lang="algol68">MODE STRING = FLEX[1:0]CHAR;</syntaxhighlight>
ALGOL 68 also has raw strings called BYTES, this type is a fixed width packed array of CHAR.
ALGOL 68 also has raw strings called BYTES, this type is a fixed width packed array of CHAR.
<lang algol68>BYTES bytesabc = bytes pack("abc");</lang>
<syntaxhighlight lang="algol68">BYTES bytesabc = bytes pack("abc");</syntaxhighlight>
A string quote character is inserted in a string when two quotes are entered, eg:
A string quote character is inserted in a string when two quotes are entered, eg:
<lang algol68>STRING stringquote = """I'll be back."" - The Terminator";</lang>
<syntaxhighlight lang="algol68">STRING stringquote = """I'll be back."" - The Terminator";</syntaxhighlight>
A string can span lines, but cannot contain newlines. String literals are concatenated when compiled:
A string can span lines, but cannot contain newlines. String literals are concatenated when compiled:
<lang algol68>STRING linexyz := "line X;" +
<syntaxhighlight lang="algol68">STRING linexyz := "line X;" +
"line Y;" +
"line Y;" +
"line Z;";</lang>
"line Z;";</syntaxhighlight>
ALGOL 68 uses FORMATs for doing more advanced manipulations.
ALGOL 68 uses FORMATs for doing more advanced manipulations.
For example given:
For example given:
<lang algol68>FILE linef; STRING line;
<syntaxhighlight lang="algol68">FILE linef; STRING line;
associate(linef, line);</lang>
associate(linef, line);</syntaxhighlight>
Instead of using preprocessor macros ALGOL 68 can do FORMAT variable replacement within FORMATs at run time.
Instead of using preprocessor macros ALGOL 68 can do FORMAT variable replacement within FORMATs at run time.
<lang algol68>FORMAT my_symbol = $"SYMBOL"$;
<syntaxhighlight lang="algol68">FORMAT my_symbol = $"SYMBOL"$;
FORMAT foo = $"prefix_"f(my_symbol)"_suffix"$;
FORMAT foo = $"prefix_"f(my_symbol)"_suffix"$;
putf(linef ,foo);</lang>
putf(linef ,foo);</syntaxhighlight>
In <b>standard</b> ALGOL 68 a "book" is a file. A book is composed of pages and lines and
In <b>standard</b> ALGOL 68 a "book" is a file. A book is composed of pages and lines and
therefore a FORMAT be used for inserting backspaces, space, newlines and newpages into books.
therefore a FORMAT be used for inserting backspaces, space, newlines and newpages into books.
<lang algol68>INT pages=100, lines=25, characters=80;
<syntaxhighlight lang="algol68">INT pages=100, lines=25, characters=80;
FILE bookf; FLEX[pages]FLEX[lines]FLEX[characters]CHAR book;
FILE bookf; FLEX[pages]FLEX[lines]FLEX[characters]CHAR book;
associate(bookf, book);
associate(bookf, book);


# following putf inserts the string " Line 4 indented 5" on page 3 #
# following putf inserts the string " Line 4 indented 5" on page 3 #
putf(bookf, $3p"Page 3"4l5x"Line 4 indented 5"$)</lang>
putf(bookf, $3p"Page 3"4l5x"Line 4 indented 5"$)</syntaxhighlight>
Note: ALGOL 68G does not implement newpage and backspace.
Note: ALGOL 68G does not implement newpage and backspace.


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
% String literals are enclosed in double-quotes in Algol W. %
% String literals are enclosed in double-quotes in Algol W. %
% There isn't a separate character type but strings of lenghth one can %
% There isn't a separate character type but strings of lenghth one can %
Line 117: Line 179:
write( "a\nb" );
write( "a\nb" );


end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 127: Line 189:
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program stringsEx.s */
/* program stringsEx.s */
Line 206: Line 268:
pop {r0,r1,r2,r7,lr} @ restaur registers
pop {r0,r1,r2,r7,lr} @ restaur registers
bx lr @ return
bx lr @ return
</syntaxhighlight>
</lang>


=={{header|Arturo}}==
=={{header|Arturo}}==
===Character literal===
<lang arturo>chr: "Z"</lang>


<syntaxhighlight lang="rebol">str: "Hello world"
===String literal===
<lang arturo>str: "Hello World"</lang>


print [str "->" type str]
===Multiline String literal===

<lang arturo>multiline: "This
fullLineStr: « This is a full-line string

print [fullLineStr "->" type fullLineStr]

multiline: {
This
is a multi-line
string
}

print [multiline "->" type multiline]

verbatim: {:
This is
a verbatim
multi-line
string
:}

print [verbatim "->" type verbatim]</syntaxhighlight>

{{out}}

<pre>Hello world -> :string
This is a full-line string -> :string
This
is a multi-line
is a multi-line
string"</lang>
string -> :string
This is
yet another
multi-line
string -> :string</pre>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
unicode
unicode
<lang AutoHotkey>"c" ; character
<syntaxhighlight lang="autohotkey">"c" ; character
"text" ; string
"text" ; string
hereString = ; with interpolation of %variables%
hereString = ; with interpolation of %variables%
Line 234: Line 323:
(Comments %
(Comments %
literal %A_Now% ; no interpolation here
literal %A_Now% ; no interpolation here
)</lang>
)</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
In awk, strings are enclosed using doublequotes.
In awk, strings are enclosed using doublequotes.
Characters are just strings of length 1.
Characters are just strings of length 1.
<lang awk> c = "x"
<syntaxhighlight lang="awk"> c = "x"
str= "hello"
str= "hello"
s1 = "abcd" # simple string
s1 = "abcd" # simple string
s2 = "ab\"cd" # string containing a double quote, escaped with backslash
s2 = "ab\"cd" # string containing a double quote, escaped with backslash
print s1
print s1
print s2 </lang>
print s2 </syntaxhighlight>


{{out}} Concatenation
{{out}} Concatenation
<lang awk>$ awk 'BEGIN{c="x"; s="hello";s1 = "abcd"; s2 = "ab\"cd"; s=s c; print s; print s1; print s2}'
<syntaxhighlight lang="awk">$ awk 'BEGIN{c="x"; s="hello";s1 = "abcd"; s2 = "ab\"cd"; s=s c; print s; print s1; print s2}'
hellox</lang>
hellox</syntaxhighlight>


=={{header|Axe}}==
=={{header|Axe}}==
Character literal:
Character literal:
<lang axe>'A'</lang>
<syntaxhighlight lang="axe">'A'</syntaxhighlight>


String literal:
String literal:
<lang axe>"ABC"</lang>
<syntaxhighlight lang="axe">"ABC"</syntaxhighlight>


Note that string literals are only null-terminated if they are assigned to a variable (e.g. Str1).
Note that string literals are only null-terminated if they are assigned to a variable (e.g. Str1).
Line 265: Line 354:
Here we use the ASCII code for doublequotes to get the characters into a string:
Here we use the ASCII code for doublequotes to get the characters into a string:


<lang basic>10 LET Q$=CHR$(34): REM DOUBLEQUOTES
<syntaxhighlight lang="basic">10 LET Q$=CHR$(34): REM DOUBLEQUOTES
20 LET D$=Q$+Q$: REM A PAIR OF DOUBLEQUOTES
20 LET D$=Q$+Q$: REM A PAIR OF DOUBLEQUOTES
30 LET S$=Q$+"THIS IS A QUOTED STRING"+Q$
30 LET S$=Q$+"THIS IS A QUOTED STRING"+Q$
40 PRINT Q$;"HELLO";Q$:REM ADD QUOTES DURING OUTPUT</lang>
40 PRINT Q$;"HELLO";Q$:REM ADD QUOTES DURING OUTPUT</syntaxhighlight>


''Most'' modern BASIC implementations don't differentiate between characters and strings -- a character is just a string of length 1.
''Most'' modern BASIC implementations don't differentiate between characters and strings -- a character is just a string of length 1.
Line 278: Line 367:
Strings can optionally be declared as being a certain length, much like C strings.
Strings can optionally be declared as being a certain length, much like C strings.


<lang qbasic>DIM c AS STRING * 1, s AS STRING
<syntaxhighlight lang="qbasic">DIM c AS STRING * 1, s AS STRING


c = "char" 'everything after the first character is silently discarded
c = "char" 'everything after the first character is silently discarded
s = "string"
s = "string"
PRINT CHR$(34); s; " data "; c; CHR$(34)</lang>
PRINT CHR$(34); s; " data "; c; CHR$(34)</syntaxhighlight>


{{out}}
{{out}}
"string data c"
"string data c"


=== {{header|Applesoft BASIC}} ===
==={{header|Applesoft BASIC}}===
<lang ApplesoftBasic>M$ = CHR$(13) : Q$ = CHR$(34)
<syntaxhighlight lang="applesoftbasic">M$ = CHR$(13) : Q$ = CHR$(34)
A$ = "THERE ARE" + M$
A$ = "THERE ARE" + M$
A$ = A$ + "NO " + Q$ + "HERE" + Q$ + " STRINGS."
A$ = A$ + "NO " + Q$ + "HERE" + Q$ + " STRINGS."
? A$</lang>
? A$</syntaxhighlight>


==={{header|IS-BASIC}}===
==={{header|IS-BASIC}}===
<lang IS-BASIC>100 PRINT CHR$(34)
<syntaxhighlight lang="is-basic">100 PRINT CHR$(34)
110 PRINT """"
110 PRINT """"
120 PRINT "This is a ""quoted string""."</lang>
120 PRINT "This is a ""quoted string""."</syntaxhighlight>


==={{header|BASIC256}}===
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">
<lang BASIC256>
print "Hello, World."
print "Hello, World."
print chr(34); "Hello, World." & chr(34)
print chr(34); "Hello, World." & chr(34)
print "Tom said," + "'The fox ran away.'"
print "Tom said," + "'The fox ran away.'"
</syntaxhighlight>
</lang>


=== {{header|ZX Spectrum Basic}} ===
==={{header|ZX Spectrum Basic}}===


The ZX Spectrum supports the use of CHR$(34).
The ZX Spectrum supports the use of CHR$(34).
Line 311: Line 400:
by adding an extra pair of doublequotes:
by adding an extra pair of doublequotes:


<lang basic>
<syntaxhighlight lang="basic">
10 REM Print some quotes
10 REM Print some quotes
20 PRINT CHR$(34)
20 PRINT CHR$(34)
Line 318: Line 407:
50 REM Output the word hello enclosed in doublequotes
50 REM Output the word hello enclosed in doublequotes
60 PRINT """Hello"""
60 PRINT """Hello"""
</syntaxhighlight>
</lang>

==={{header|uBasic/4tH}}===

uBasic/4tH supports the inclusion of doublequotes by allowing the escape <code>\q</code> in almost every string literal.
<syntaxhighlight lang="text">Print "This is a ";Chr(Ord("\q"));"quoted string";Chr(Ord("\q"))
Print "This is a \qquoted string\q"
a := "This is a \qquoted string\q" : Print Show(a)
</syntaxhighlight>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
Line 325: Line 422:
There is no special representation for a single character (it is just a string of length one).
There is no special representation for a single character (it is just a string of length one).
'Here strings' are not supported.
'Here strings' are not supported.
<lang bbcbasic> PRINT "This is a ""quoted string"""</lang>
<syntaxhighlight lang="bbcbasic"> PRINT "This is a ""quoted string"""</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 342: Line 439:
which may be pushed using <tt>57*1-</tt>.
which may be pushed using <tt>57*1-</tt>.
Note: since you are pushing the string onto a stack, you usually want to define the string in reverse order so that the first character is on top.
Note: since you are pushing the string onto a stack, you usually want to define the string in reverse order so that the first character is on top.
<lang befunge>"gnirts">:#,_@</lang>
<syntaxhighlight lang="befunge">"gnirts">:#,_@</syntaxhighlight>


=={{header|Bracmat}}==
=={{header|Bracmat}}==
Line 373: Line 470:
In C, single characters are contained in single quotes.
In C, single characters are contained in single quotes.


<lang c>char ch = 'z';</lang>
<syntaxhighlight lang="c">char ch = 'z';</syntaxhighlight>


Strings are contained in double quotes.
Strings are contained in double quotes.


<lang c>char str[] = "z";</lang>
<syntaxhighlight lang="c">char str[] = "z";</syntaxhighlight>


This means that 'z' and "z" are different.
This means that 'z' and "z" are different.
Line 385: Line 482:


A string can span lines. Newlines can be added via backslash escapes, and string literals are concatenated when compiled:
A string can span lines. Newlines can be added via backslash escapes, and string literals are concatenated when compiled:
<lang c>char lines[] = "line 1\n"
<syntaxhighlight lang="c">char lines[] = "line 1\n"
"line 2\n"
"line 2\n"
"line 3\n";</lang>
"line 3\n";</syntaxhighlight>


C can use library functions such as ''sprintf'' for doing formatted replacement within strings at run time, or preprocessor concatenation to build string literals at compile time:
C can use library functions such as ''sprintf'' for doing formatted replacement within strings at run time, or preprocessor concatenation to build string literals at compile time:
<lang c>#define FOO "prefix_"##MY_SYMBOL##"_suffix"</lang>
<syntaxhighlight lang="c">#define FOO "prefix_"##MY_SYMBOL##"_suffix"</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
Line 398: Line 495:
'''C#''' supports verbatim strings. These begin with @" and end with ". Verbatim quotes may contain line breaks and so verbatim strings and here-strings overlap.
'''C#''' supports verbatim strings. These begin with @" and end with ". Verbatim quotes may contain line breaks and so verbatim strings and here-strings overlap.


<lang csharp>string path = @"C:\Windows\System32";
<syntaxhighlight lang="csharp">string path = @"C:\Windows\System32";
string multiline = @"Line 1.
string multiline = @"Line 1.
Line 2.
Line 2.
Line 3.";</lang>
Line 3.";</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
Line 409: Line 506:
In C++11, it is also possible to use so-called "Raw Strings":
In C++11, it is also possible to use so-called "Raw Strings":


<lang cpp>auto strA = R"(this is
<syntaxhighlight lang="cpp">auto strA = R"(this is
a newline-separated
a newline-separated
raw string)";
raw string)";
</syntaxhighlight>
</LANG>


=={{header|Clojure}}==
=={{header|Clojure}}==
Character literals are prefixed by a backslash:
Character literals are prefixed by a backslash:
<lang lisp>[\h \e \l \l \o] ; a vector of characters
<syntaxhighlight lang="lisp">[\h \e \l \l \o] ; a vector of characters
\uXXXX ; where XXXX is some hex Unicode code point
\uXXXX ; where XXXX is some hex Unicode code point
\\ ; the backslash character literal</lang>
\\ ; the backslash character literal</syntaxhighlight>
There are also identifiers for special characters:
There are also identifiers for special characters:
<lang lisp>\space
<syntaxhighlight lang="lisp">\space
\newline
\newline
\tab
\tab
\formfeed
\formfeed
\return
\return
\backspace</lang>
\backspace</syntaxhighlight>
Clojure strings ''are'' Java Strings, and literals are written in the same manner:
Clojure strings ''are'' Java Strings, and literals are written in the same manner:
<lang lisp>"hello world\r\n"</lang>
<syntaxhighlight lang="lisp">"hello world\r\n"</syntaxhighlight>


=={{header|COBOL}}==
=={{header|COBOL}}==
Strings can be enclosed in either single quotes or double quotes. There is no difference between them.
Strings can be enclosed in either single quotes or double quotes. There is no difference between them.
<lang cobol>"This is a valid string."
<syntaxhighlight lang="cobol">"This is a valid string."
'As is this.'</lang>
'As is this.'</syntaxhighlight>


Character literals are strings of two-digit hexadecimal numbers preceded by an x.
Character literals are strings of two-digit hexadecimal numbers preceded by an x.
<lang cobol>X"00" *> Null character
<syntaxhighlight lang="cobol">X"00" *> Null character
X"48656C6C6F21" *> "Hello!"</lang>
X"48656C6C6F21" *> "Hello!"</syntaxhighlight>


There are also figurative constants which are equivalent to certain string literals:
There are also figurative constants which are equivalent to certain string literals:
<lang cobol>HIGH-VALUE HIGH-VALUES *> Equivalent to (a string of) X"FF".
<syntaxhighlight lang="cobol">HIGH-VALUE HIGH-VALUES *> Equivalent to (a string of) X"FF".
LOW-VALUE LOW-VALUES *> " " X"00".
LOW-VALUE LOW-VALUES *> " " X"00".
NULL *> " " X"00".
NULL *> " " X"00".
QUOTE QUOTES *> " " double-quote character.
QUOTE QUOTES *> " " double-quote character.
SPACE SPACES *> " " space.
SPACE SPACES *> " " space.
ZERO ZEROS ZEROES *> " " zero.</lang>
ZERO ZEROS ZEROES *> " " zero.</syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Character literals are referenced using a hash-backslash notation. Strings are arrays or sequences of characters and can be declared using double-quotes or constructed using other sequence commands.
Character literals are referenced using a hash-backslash notation. Strings are arrays or sequences of characters and can be declared using double-quotes or constructed using other sequence commands.
<lang lisp>(let ((colon #\:)
<syntaxhighlight lang="lisp">(let ((colon #\:)
(str "http://www.rosettacode.com/"))
(str "http://www.rosettacode.com/"))
(format t "colon found at position ~d~%" (position colon str)))</lang>
(format t "colon found at position ~d~%" (position colon str)))</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
Character literals:
Character literals:


<lang d>char c = 'a';</lang>
<syntaxhighlight lang="d">char c = 'a';</syntaxhighlight>


Regular strings support C-style escape sequences.
Regular strings support C-style escape sequences.


<lang d>auto str = "hello"; // UTF-8
<syntaxhighlight lang="d">auto str = "hello"; // UTF-8
auto str2 = "hello"c; // UTF-8
auto str2 = "hello"c; // UTF-8
auto str3 = "hello"w; // UTF-16
auto str3 = "hello"w; // UTF-16
auto str4 = "hello"d; // UTF-32</lang>
auto str4 = "hello"d; // UTF-32</syntaxhighlight>


Literal string (escape sequences are not interpreted):
Literal string (escape sequences are not interpreted):


<lang d>auto str = `"Hello," he said.`;
<syntaxhighlight lang="d">auto str = `"Hello," he said.`;
auto str2 = r"\n is slash-n";</lang>
auto str2 = r"\n is slash-n";</syntaxhighlight>


Specified delimiter string:
Specified delimiter string:


<lang d>// Any character is allowed after the first quote;
<syntaxhighlight lang="d">// Any character is allowed after the first quote;
// the string ends with that same character followed
// the string ends with that same character followed
// by a quote.
// by a quote.
auto str = q"$"Hello?" he enquired.$";</lang>
auto str = q"$"Hello?" he enquired.$";</syntaxhighlight>


<lang d>// If you include a newline, you get a heredoc string:
<syntaxhighlight lang="d">// If you include a newline, you get a heredoc string:
auto otherStr = q"EOS
auto otherStr = q"EOS
This is part of the string.
This is part of the string.
So is this.
So is this.
EOS";</lang>
EOS";</syntaxhighlight>


Token string:
Token string:


<lang d>// The contents of a token string must be valid code fragments.
<syntaxhighlight lang="d">// The contents of a token string must be valid code fragments.
auto str = q{int i = 5;};
auto str = q{int i = 5;};
// The contents here isn't a legal token in D, so it's an error:
// The contents here isn't a legal token in D, so it's an error:
auto illegal = q{@?};</lang>
auto illegal = q{@?};</syntaxhighlight>


Hex string:
Hex string:


<lang d>// assigns value 'hello' to str
<syntaxhighlight lang="d">// assigns value 'hello' to str
auto str = x"68 65 6c 6c 6f";</lang>
auto str = x"68 65 6c 6c 6f";</syntaxhighlight>


=={{header|Delphi}}==
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">var
<lang Delphi>var
lChar: Char;
lChar: Char;
lLine: string;
lLine: string;
Line 502: Line 599:
lChar := 'a';
lChar := 'a';
lLine := 'some text';
lLine := 'some text';
lMultiLine := 'some text' + #13#10 + 'on two lines';</lang>
lMultiLine := 'some text' + #13#10 + 'on two lines';</syntaxhighlight>


=={{header|DWScript}}==
=={{header|DWScript}}==
Strings are either single or double quote delimited, if you want to include the delimiter in the string, you just double it.
Strings are either single or double quote delimited, if you want to include the delimiter in the string, you just double it.
Specific character codes (Unicode) can be specified via # (outside of the string).
Specific character codes (Unicode) can be specified via # (outside of the string).
<lang delphi>
<syntaxhighlight lang="delphi">
const s1 := 'quoted "word" in string';
const s1 := 'quoted "word" in string';
const s2 := "quoted ""word"" in string"; // sames as s1, shows the doubling of the delimiter
const s2 := "quoted ""word"" in string"; // sames as s1, shows the doubling of the delimiter
const s2 := 'first line'#13#10'second line'; // CR+LF in the middle
const s2 := 'first line'#13#10'second line'; // CR+LF in the middle
</syntaxhighlight>
</lang>


=={{header|Dyalect}}==
=={{header|Dyalect}}==
Line 517: Line 614:
Strings in Dyalect are double quote delimited (and characters are single quote delimited). Both support escape codes:
Strings in Dyalect are double quote delimited (and characters are single quote delimited). Both support escape codes:


<lang Dyalect>var c = '\u0020' //a character
<syntaxhighlight lang="dyalect">let c = '\u0020' //a character
var str = "A string\non several lines!\sAnd you can incorporate expressions: \(c)!"</lang>
let str = "A string\non several lines!\sAnd you can incorporate expressions: \(c)!"</syntaxhighlight>


Dyalect also supports multiline strings:
Multiline strings are not currently supported.

<syntaxhighlight lang="dyalect">let long_str = <[first line
second line
third line]></syntaxhighlight>

Multiline strings do not support escape codes.


=={{header|Déjà Vu}}==
=={{header|Déjà Vu}}==
<lang dejavu>local :s "String literal"
<syntaxhighlight lang="dejavu">local :s "String literal"
local :s2 "newline \n carriage return \r tab \t"
local :s2 "newline \n carriage return \r tab \t"
!print "backslash \\ quote \q decimal character \{8364}"</lang>
!print "backslash \\ quote \q decimal character \{8364}"</syntaxhighlight>
{{out}}
{{out}}
<pre>backslash \ quote " decimal character €</pre>
<pre>backslash \ quote " decimal character €</pre>
Line 533: Line 636:
E has three sorts of quotes: ''strings'', ''characters'', and ''quasiliterals''.
E has three sorts of quotes: ''strings'', ''characters'', and ''quasiliterals''.


<lang e>'T' # character
<syntaxhighlight lang="e">'T' # character
"The quick brown fox" # string
"The quick brown fox" # string
`The $kind brown fox` # "simple" quasiliteral
`The $kind brown fox` # "simple" quasiliteral
term`the($adjectives*, fox)` # "term" quasiliteral</lang>
term`the($adjectives*, fox)` # "term" quasiliteral</syntaxhighlight>


Strings and characters use syntax similar to Java; double and single quotes, respectively, and common backslash escapes.
Strings and characters use syntax similar to Java; double and single quotes, respectively, and common backslash escapes.
Line 544: Line 647:
Quasiliterals can be used for strings as well. The third example above is the built-in simple interpolator, which also supports pattern matching. There is also a regular-expression quasi-pattern:
Quasiliterals can be used for strings as well. The third example above is the built-in simple interpolator, which also supports pattern matching. There is also a regular-expression quasi-pattern:


<lang e>? if ("<abc,def>" =~ `<@a,@b>`) { [a, b] } else { null }
<syntaxhighlight lang="e">? if ("<abc,def>" =~ `<@a,@b>`) { [a, b] } else { null }
# value: ["abc", "def"]
# value: ["abc", "def"]


? if (" >abc, def< " =~ rx`\W*(@a\w+)\W+(@b\w+)\W*`) { [a, b] } else { null }
? if (" >abc, def< " =~ rx`\W*(@a\w+)\W+(@b\w+)\W*`) { [a, b] } else { null }
# value: ["abc", "def"]</lang>
# value: ["abc", "def"]</syntaxhighlight>

=={{header|EasyLang}}==
Strings are always enclosed in double quotes ("). Unicode is also supported.
<syntaxhighlight lang="easylang">
print "EasyLang"
print "简"
</syntaxhighlight>


=={{header|Ela}}==
=={{header|Ela}}==
Ela has both characters:
Ela has both characters:
<lang ela>c = 'c'</lang>
<syntaxhighlight lang="ela">c = 'c'</syntaxhighlight>
and strings:
and strings:
<lang ela>str = "Hello, world!"</lang>
<syntaxhighlight lang="ela">str = "Hello, world!"</syntaxhighlight>
Both support C-style escape codes:
Both support C-style escape codes:
<lang ela>c = '\t'
<syntaxhighlight lang="ela">c = '\t'
str = "first line\nsecond line\nthird line"</lang>
str = "first line\nsecond line\nthird line"</syntaxhighlight>
Also Ela supports verbatim strings with the following syntax:
Also Ela supports verbatim strings with the following syntax:
<lang ela>vs = <[This is a
<syntaxhighlight lang="ela">vs = <[This is a
verbatim string]></lang>
verbatim string]></syntaxhighlight>


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 4.x :
ELENA 4.x :
<lang elena>
<syntaxhighlight lang="elena">
var c := $65; // character
var c := $65; // character
var s := "some text"; // UTF-8 literal
var s := "some text"; // UTF-8 literal
Line 570: Line 680:
var s2 := "text with ""quotes"" and
var s2 := "text with ""quotes"" and
two lines";
two lines";
</syntaxhighlight>
</lang>


=={{header|Elixir}}==
=={{header|Elixir}}==
===String===
===String===
Strings are between double quotes; they're represented internally as utf-8 encoded bytes and support interpolation.
Strings are between double quotes; they're represented internally as utf-8 encoded bytes and support interpolation.
<syntaxhighlight lang="elixir">
<lang Elixir>
IO.puts "Begin String \n============"
IO.puts "Begin String \n============"
str = "string"
str = "string"
str |> is_binary # true
str |> is_binary # true
</syntaxhighlight>
</lang>
While internally represented as a sequence of bytes, the String module splits the codepoints into strings.
While internally represented as a sequence of bytes, the String module splits the codepoints into strings.
<syntaxhighlight lang="elixir">
<lang Elixir>
str |> String.codepoints
str |> String.codepoints
</syntaxhighlight>
</lang>
The bytes can be accessed by appending a null byte to the string
The bytes can be accessed by appending a null byte to the string
<syntaxhighlight lang="elixir">
<lang Elixir>
str <> <<0>>
str <> <<0>>
</syntaxhighlight>
</lang>
Strings can be evaluated using <code>?</code> before a character in the command line or in a string, then evaluating the string
Strings can be evaluated using <code>?</code> before a character in the command line or in a string, then evaluating the string
<syntaxhighlight lang="elixir">
<lang Elixir>
?a # 97
?a # 97
Code.eval_string("?b") # 98
Code.eval_string("?b") # 98
Code.eval_string("?ł") # 322
Code.eval_string("?ł") # 322
</syntaxhighlight>
</lang>


===Char Lists===
===Char Lists===
Char lists are simply lists of characters. Elixir will attempt to convert number values to characters if a string could be formed from the values. Char lists represent characters as single quotes and still allow for interpolation.
Char lists are simply lists of characters. Elixir will attempt to convert number values to characters if a string could be formed from the values. Char lists represent characters as single quotes and still allow for interpolation.
<syntaxhighlight lang="elixir">
<lang Elixir>
IO.inspect "Begin Char List \n============="
IO.inspect "Begin Char List \n============="
[115, 116, 114, 105, 110, 103]
[115, 116, 114, 105, 110, 103]
ch = "hi"
ch = "hi"
'string #{ch}'
'string #{ch}'
</syntaxhighlight>
</lang>
Again, since 0 cannot be rendered as a character, adding it to a char list will return the char list
Again, since 0 cannot be rendered as a character, adding it to a char list will return the char list
<syntaxhighlight lang="elixir">
<lang Elixir>
'string #{ch}'++[0]
'string #{ch}'++[0]
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 628: Line 738:
The only string literal is a double-quote
The only string literal is a double-quote


<lang Lisp>"This is a string."</lang>
<syntaxhighlight lang="lisp">"This is a string."</syntaxhighlight>


Backslash gives various special characters similar to C, such as
Backslash gives various special characters similar to C, such as
Line 639: Line 749:
was a separate type.) <code>?</code> is the read syntax.
was a separate type.) <code>?</code> is the read syntax.


<lang Lisp>?z => 122
<syntaxhighlight lang="lisp">?z ;=> 122
?\n => 10</lang>
?\n ;=> 10</syntaxhighlight>


See "Basic Char Syntax" in the elisp manual.
See "Basic Char Syntax" in the elisp manual.
Line 646: Line 756:
=={{header|Erlang}}==
=={{header|Erlang}}==
Erlang strings are lists containing integer values within the range of the ASCII or (depending on version and settings) Unicode characters.
Erlang strings are lists containing integer values within the range of the ASCII or (depending on version and settings) Unicode characters.
<lang erlang>
<syntaxhighlight lang="erlang">
"This is a string".
"This is a string".
[$T,$h,$i,$s,$ ,$a,$ ,$s,$t,$r,$i,$n,$g,$,,$ ,$t,$o,$o].
[$T,$h,$i,$s,$ ,$a,$ ,$s,$t,$r,$i,$n,$g,$,,$ ,$t,$o,$o].
</syntaxhighlight>
</lang>
Characters are represented either as literals (above) or integer values.
Characters are represented either as literals (above) or integer values.
<lang erlang>
<syntaxhighlight lang="erlang">
97 == $a. % => true
97 == $a. % => true
</syntaxhighlight>
</lang>
With the string syntax, characters can be escaped with \.
With the string syntax, characters can be escaped with \.
<lang erlang>
<syntaxhighlight lang="erlang">
"\"The quick brown fox jumps over the lazy dog.\"".
"\"The quick brown fox jumps over the lazy dog.\"".
</syntaxhighlight>
</lang>


=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
let n= 'N'
let i="Name=\"Nigel Galloway\"\n"
let g= @"Name=""Nigel Galloway""\n"
let e="Nigel
Galloway"
let l= """Name="Nigel Galloway"\n"""
printfn "%c\n%s\n%s\n%s\n%s" n i g e l
</syntaxhighlight>
{{out}}
<pre>
N
Name="Nigel Galloway"

Name="Nigel Galloway"\n
Nigel
Galloway
Name="Nigel Galloway"\n
</pre>
=={{header|Factor}}==
=={{header|Factor}}==
A basic character:
A basic character:
<lang factor>CHAR: a</lang>
<syntaxhighlight lang="factor">CHAR: a</syntaxhighlight>
Characters are Unicode code points (integers in the range <tt>[0-2,097,152]</tt>).
Characters are Unicode code points (integers in the range <tt>[0-2,097,152]</tt>).


<code>CHAR:</code> is a parsing word that takes a literal character, escape code, or Unicode code point name and adds a Unicode code point to the parse tree.
<code>CHAR:</code> is a parsing word that takes a literal character, escape code, or Unicode code point name and adds a Unicode code point to the parse tree.
<lang factor>CHAR: x ! 120
<syntaxhighlight lang="factor">CHAR: x ! 120
CHAR: \u000032 ! 50
CHAR: \u000032 ! 50
CHAR: \u{exclamation-mark} ! 33
CHAR: \u{exclamation-mark} ! 33
CHAR: exclamation-mark ! 33
CHAR: exclamation-mark ! 33
CHAR: ugaritic-letter-samka ! 66450</lang>
CHAR: ugaritic-letter-samka ! 66450</syntaxhighlight>


Strings are represented as fixed-size mutable sequences of Unicode code points.
Strings are represented as fixed-size mutable sequences of Unicode code points.


A basic string:
A basic string:
<lang factor>"Hello, world!"</lang>
<syntaxhighlight lang="factor">"Hello, world!"</syntaxhighlight>


We can take a look under the hood:
We can take a look under the hood:
<lang factor>"Hello, world!" { } like ! { 72 101 108 108 111 44 32 119 111 114 108 100 33 }</lang>
<syntaxhighlight lang="factor">"Hello, world!" { } like ! { 72 101 108 108 111 44 32 119 111 114 108 100 33 }</syntaxhighlight>


Both <code>CHAR:</code> and strings support the following escape codes:
Both <code>CHAR:</code> and strings support the following escape codes:
Line 730: Line 860:


Some examples of strings with escape codes:
Some examples of strings with escape codes:
<lang factor>"Line one\nLine two" print</lang>
<syntaxhighlight lang="factor">"Line one\nLine two" print</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 737: Line 867:
</pre>
</pre>
Putting quotation marks into a string:
Putting quotation marks into a string:
<lang factor>"\"Hello,\" she said." print</lang>
<syntaxhighlight lang="factor">"\"Hello,\" she said." print</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 743: Line 873:
</pre>
</pre>
Strings can span multiple lines. Newlines are inserted where they occur in the literal.
Strings can span multiple lines. Newlines are inserted where they occur in the literal.
<lang factor>"2\u{superscript-two} = 4
<syntaxhighlight lang="factor">"2\u{superscript-two} = 4
2\u{superscript-three} = 8
2\u{superscript-three} = 8
2\u{superscript-four} = 16" print</lang>
2\u{superscript-four} = 16" print</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 755: Line 885:


A verbatim string:
A verbatim string:
<lang factor>USE: multiline
<syntaxhighlight lang="factor">USE: multiline
[[ escape codes \t are literal \\ in here
[[ escape codes \t are literal \\ in here
but newlines \u{plus-minus-sign} are still
but newlines \u{plus-minus-sign} are still
inserted " for each line the string \" spans.]] print</lang>
inserted " for each line the string \" spans.]] print</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 768: Line 898:


A here-string:
A here-string:
<lang factor>USE: multiline
<syntaxhighlight lang="factor">USE: multiline
HEREDOC: END
HEREDOC: END
Everything between the line above
Everything between the line above
Line 775: Line 905:
is significant.
is significant.
END
END
print</lang>
print</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 785: Line 915:
</pre>
</pre>
<code>STRING:</code> is similar to <code>HEREDOC:</code> except instead of immediately placing the string on the data stack, it defines a word that places the string on the data stack when called.
<code>STRING:</code> is similar to <code>HEREDOC:</code> except instead of immediately placing the string on the data stack, it defines a word that places the string on the data stack when called.
<lang factor>USE: multiline
<syntaxhighlight lang="factor">USE: multiline
STRING: random-stuff
STRING: random-stuff
ABC
ABC
Line 791: Line 921:
"x y z
"x y z
;
;
random-stuff print</lang>
random-stuff print</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 800: Line 930:


Finally, the <code>interpolate</code> vocabulary provides support for interpolating lexical variables, dynamic variables, and data stack values into strings.
Finally, the <code>interpolate</code> vocabulary provides support for interpolating lexical variables, dynamic variables, and data stack values into strings.
<lang factor>USING: interpolate locals namespaces ;
<syntaxhighlight lang="factor">USING: interpolate locals namespaces ;


"Sally" "name" set
"Sally" "name" set
Line 814: Line 944:
I]
I]


]</lang>
]</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 824: Line 954:
=={{header|Forth}}==
=={{header|Forth}}==
In the interpreter:
In the interpreter:
<lang forth>char c emit
<syntaxhighlight lang="forth">char c emit
s" string" type</lang>
s" string" type</syntaxhighlight>
In the compiler:
In the compiler:
<lang forth>: main
<syntaxhighlight lang="forth">: main
[char] c emit
[char] c emit
s" string" type ;</lang>
s" string" type ;</syntaxhighlight>
Strings may contain any printable character except a double quote, and may not span multiple lines. Strings are done via the word S" which parses ahead for a terminal quote. The space directly after S" is thus not included in the string.
Strings may contain any printable character except a double quote, and may not span multiple lines. Strings are done via the word S" which parses ahead for a terminal quote. The space directly after S" is thus not included in the string.


Line 835: Line 965:


GNU Forth has a prefix syntax for character literals, and another string literal word S\" which allows escaped characters, similar to [[C]].
GNU Forth has a prefix syntax for character literals, and another string literal word S\" which allows escaped characters, similar to [[C]].
<lang forth>'c emit
<syntaxhighlight lang="forth">'c emit
s\" hello\nthere!"</lang>
s\" hello\nthere!"</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
First Fortran (1958) did not offer any means to manipulate text except via the H (for Hollerith) code in FORMAT statements of the form nH where n was an integer that counted the ''exact'' numbers of characters following the H, any characters, that constituted the text literal. Miscounts would cause a syntax error, if you were lucky. This would be used for output to annotate the numbers, but consider the following: <lang Fortran> DIMENSION ATWT(12)
First Fortran (1958) did not offer any means to manipulate text except via the H (for Hollerith) code in FORMAT statements of the form nH where n was an integer that counted the ''exact'' numbers of characters following the H, any characters, that constituted the text literal. Miscounts would cause a syntax error, if you were lucky. This would be used for output to annotate the numbers, but consider the following: <syntaxhighlight lang="fortran"> DIMENSION ATWT(12)
PRINT 1
PRINT 1
1 FORMAT (12HElement Name,F9.4)
1 FORMAT (12HElement Name,F9.4)
Line 845: Line 975:
READ 1,ATWT(I)
READ 1,ATWT(I)
10 PRINT 1,ATWT(I)
10 PRINT 1,ATWT(I)
END </lang>
END </syntaxhighlight>
Evidently, the syntax highlighter here does not recognise the Hollerith style usage. Nor do some compilers, even if in its original home within FORMAT statements.
Evidently, the syntax highlighter here does not recognise the Hollerith style usage. Nor do some compilers, even if in its original home within FORMAT statements.


The first PRINT statement writes out a heading, here with lower case letters as an anachronism. Then the loop reads a deck of cards containing the name of an element and its atomic weight into an array ATWT, but the special feature is that the first twelve characters of each card replace the text in the FORMAT statement, and thus the following PRINT statement shows the name of the element followed by its atomic weight as just read.
The first PRINT statement writes out a heading, here with lower case letters as an anachronism. Then the loop reads a deck of cards containing the name of an element and its atomic weight into an array ATWT, but the special feature is that the first twelve characters of each card replace the text in the FORMAT statement, and thus the following PRINT statement shows the name of the element followed by its atomic weight as just read.


Fortran IV introduced a text literal, specified within apostrophes, with two apostrophes in a row indicating an apostrophe within the text. Later, either an apostrophe or a double quote could be used to start a text string (and the same one must be used to end it) so that if one or the other were desired within a text literal, the other could be used as its delimiters. If both were desired, then there would be no escape from doubling for one. Because spaces are significant within text literals, a long text literal continued on the next line would have the contents of column seven of the continuation line immediately following the contents of column 72 of the continued line - except that (for some compilers reading disc files) if such lines did not extend to column 72 (because trailing spaces were trimmed from the records) rather less text would be defined. So, even though this is in fixed-format (or card image) style, again misinterpreted by the syntax highlighter, <lang Fortran> BLAH = "
Fortran IV introduced a text literal, specified within apostrophes, with two apostrophes in a row indicating an apostrophe within the text. Later, either an apostrophe or a double quote could be used to start a text string (and the same one must be used to end it) so that if one or the other were desired within a text literal, the other could be used as its delimiters. If both were desired, then there would be no escape from doubling for one. Because spaces are significant within text literals, a long text literal continued on the next line would have the contents of column seven of the continuation line immediately following the contents of column 72 of the continued line - except that (for some compilers reading disc files) if such lines did not extend to column 72 (because trailing spaces were trimmed from the records) rather less text would be defined. So, even though this is in fixed-format (or card image) style, again misinterpreted by the syntax highlighter, <syntaxhighlight lang="fortran"> BLAH = "
1Stuff"</lang>
1Stuff"</syntaxhighlight>
might be the equivalent of only <code>BLAH = "Stuff"</code> instead of defining a text literal with many leading spaces. F90 formalised an opportunity for free-format source files; many compilers had also allowed usage beyond column 72.
might be the equivalent of only <code>BLAH = "Stuff"</code> instead of defining a text literal with many leading spaces. F90 formalised an opportunity for free-format source files; many compilers had also allowed usage beyond column 72.


Within the text literal, any character whatever may be supplied as text grist, according to the encodement recognised by the card reader as this was a fixed-format file - cards have an actual physical size. This applied also to source text held in disc files, as they were either fixed-size records or, for variable-length records, records had a length specification and the record content was not involved. Variable-length records were good for omitting the storage of the trailing spaces on each line, except that the sequence numbers were at the end of the line! In this case they might be omitted (unlike a card deck, a disc file's records are not going to be dropped) or there may be special provision for them to be at the start of each line with the source text's column one staring in column nine of the record. But, for the likes of paper tape, the question "How long is a record?" has no natural answer, and record endings were marked by a special symbol. Such a symbol (or symbol sequence) could not appear within a record, such as within a text literal and be taken as a part of the text. This style has been followed by the ASCII world, with variously CR, CRLF, LFCR and CR sequences being used to mark end-of-record. Such characters cannot be placed within a text literal, but the CHAR(n) function makes them available in character expressions. Some compilers however corrupt the "literal" nature of text ''literals'' by allowing escape sequences to do so, usually in the style popularised by C, thus \n, and consequently, \\ should a single \ be desired.
Within the text literal, any character whatever may be supplied as text grist, according to the encodement recognised by the card reader as this was a fixed-format file - cards have an actual physical size. This applied also to source text held in disc files, as they were either fixed-size records or, for variable-length records, records had a length specification and the record content was not involved. Variable-length records were good for omitting the storage of the trailing spaces on each line, except that the sequence numbers were at the end of the line! In this case they might be omitted (unlike a card deck, a disc file's records are not going to be dropped) or there may be special provision for them to be at the start of each line with the source text's column one staring in column nine of the record. But, for the likes of paper tape, the question "How long is a record?" has no natural answer, and record endings were marked by a special symbol. Such a symbol (or symbol sequence) could not appear within a record, such as within a text literal and be taken as a part of the text. This style has been followed by the ASCII world, with variously CR, CRLF, LFCR and CR sequences being used to mark end-of-record. Such characters cannot be placed within a text literal, but the CHAR(n) function makes them available in character expressions. Some compilers however corrupt the "literal" nature of text ''literals'' by allowing escape sequences to do so, usually in the style popularised by C, thus \n, and consequently, \\ should a single \ be desired.


Some examples, supposing that TEXT is a CHARACTER variable. <lang Fortran> TEXT = 'That''s right!' !Only apostrophes as delimiters. Doubling required.
Some examples, supposing that TEXT is a CHARACTER variable. <syntaxhighlight lang="fortran"> TEXT = 'That''s right!' !Only apostrophes as delimiters. Doubling required.
TEXT = "That's right!" !Chose quotes, so that apostrophes may be used freely.
TEXT = "That's right!" !Chose quotes, so that apostrophes may be used freely.
TEXT = "He said ""That's right!""" !Give in, and use quotes for a "quoted string" source style.
TEXT = "He said ""That's right!""" !Give in, and use quotes for a "quoted string" source style.
TEXT = 'He said "That''s right!"' !Though one may dabble in inconsistency.
TEXT = 'He said "That''s right!"' !Though one may dabble in inconsistency.
TEXT = 23HHe said "That's right!" !Some later compilers allowed Hollerith to escape from FORMAT. </lang>
TEXT = 23HHe said "That's right!" !Some later compilers allowed Hollerith to escape from FORMAT. </syntaxhighlight>


A similar syntax enables the specification of hexadecimal, octal or binary sequences, as in <code>X = Z"01FE"</code> for hexadecimal (the "H" code already being used for "Hollerith" even if the H-usage is not supported by the compiler) but this is for numerical values, not text strings. While one could mess about with EQUIVALENCE statements, numbers fill up from the right while text strings fill from the left and there would be "endian" issues as well, so it is probably not worth the bother. Just use the CHAR function in an expression, as in
A similar syntax enables the specification of hexadecimal, octal or binary sequences, as in <code>X = Z"01FE"</code> for hexadecimal (the "H" code already being used for "Hollerith" even if the H-usage is not supported by the compiler) but this is for numerical values, not text strings. While one could mess about with EQUIVALENCE statements, numbers fill up from the right while text strings fill from the left and there would be "endian" issues as well, so it is probably not worth the bother. Just use the CHAR function in an expression, as in
<lang Fortran> TEXT = "That's"//CHAR(10)//"right!" !For an ASCII linefeed (or newline) character.</lang>
<syntaxhighlight lang="fortran"> TEXT = "That's"//CHAR(10)//"right!" !For an ASCII linefeed (or newline) character.</syntaxhighlight>
Which may or may not be acted upon by the output device. A lineprinter probably would ignore a linefeed character but a teletype would not - it would roll the printing carriage one line up without returning to the column one position, thus the usage LFCR (or CRLF) to add the carriage return action. Some systems regard the LF as also implying a CR and for these the notation \n for "newline" is mnemonic even though there is no "newline" character code in ASCII - though there is in EBCDIC. Display screens do not handle glyph construction via overprinting though teletypes (and lineprinters) do. Similarly, a display screen may or may not start a new screen with a formfeed character and a lineprinter won't start a new page - at least if attached to a mainframe computer.
Which may or may not be acted upon by the output device. A lineprinter probably would ignore a linefeed character but a teletype would not - it would roll the printing carriage one line up without returning to the column one position, thus the usage LFCR (or CRLF) to add the carriage return action. Some systems regard the LF as also implying a CR and for these the notation \n for "newline" is mnemonic even though there is no "newline" character code in ASCII - though there is in EBCDIC. Display screens do not handle glyph construction via overprinting though teletypes (and lineprinters) do. Similarly, a display screen may or may not start a new screen with a formfeed character and a lineprinter won't start a new page - at least if attached to a mainframe computer.


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
Print "Hello, World."
Print "Hello, World."
Print Chr(34); "Hello, World." & Chr(34)
Print Chr(34); "Hello, World." & Chr(34)
Line 873: Line 1,003:
Print "Tom said, ""The fox ran away."""
Print "Tom said, ""The fox ran away."""
Print "Tom said," + "'The fox ran away.'"
Print "Tom said," + "'The fox ran away.'"
</syntaxhighlight>
</lang>


=={{header|friendly interactive shell}}==
=={{header|friendly interactive shell}}==
<lang fishshell>echo Quotes are optional in most cases.
<syntaxhighlight lang="fishshell">echo Quotes are optional in most cases.
echo
echo
echo 'But they are when using either of these characters (or whitespace):'
echo 'But they are when using either of these characters (or whitespace):'
Line 885: Line 1,015:
echo
echo
set something variable
set something variable
echo "Double quotes interpolates \\, \" and \$ sequences and $something accesses."</lang>
echo "Double quotes interpolates \\, \" and \$ sequences and $something accesses."</syntaxhighlight>


=={{header|FurryScript}}==
=={{header|FurryScript}}==
Line 896: Line 1,026:


All three kinds are string literals.
All three kinds are string literals.

=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
@"Hello, world!"
</syntaxhighlight>


=={{header|GAP}}==
=={{header|GAP}}==
<lang gap>IsChar('a');
<syntaxhighlight lang="gap">IsChar('a');
# true
# true
IsString("abc");
IsString("abc");
Line 905: Line 1,040:
# false
# false
IsChar("a");
IsChar("a");
# false</lang>
# false</syntaxhighlight>


=={{header|gecho}}==
=={{header|gecho}}==
<lang gecho>'a outascii</lang>
<syntaxhighlight lang="gecho">'a outascii</syntaxhighlight>
Just one character.
Just one character.
<lang gecho>'yo...dawg. print</lang>
<syntaxhighlight lang="gecho">'yo...dawg. print</syntaxhighlight>
A string.
A string.


Line 918: Line 1,053:
In Go, character literals are called "rune literals" and can be any single valid Unicode code point.
In Go, character literals are called "rune literals" and can be any single valid Unicode code point.
They are written as an integer value or as text within single quotes.
They are written as an integer value or as text within single quotes.
<lang go>ch := 'z'
<syntaxhighlight lang="go">ch := 'z'
ch = 122 // or 0x7a or 0172 or any other integer literal
ch = 122 // or 0x7a or 0172 or any other integer literal
ch = '\x7a' // \x{2*hex}
ch = '\x7a' // \x{2*hex}
ch = '\u007a' // \u{4*hex}
ch = '\u007a' // \u{4*hex}
ch = '\U0000007a' // \U{8*hex}
ch = '\U0000007a' // \U{8*hex}
ch = '\172' // \{3*octal}</lang>
ch = '\172' // \{3*octal}</syntaxhighlight>


A rune literal results in an untyped integer.
A rune literal results in an untyped integer.
When used in a typed constant or stored in a variable, usually the type is either <code>byte</code> or <code>rune</code> to distinguish character values from integer values.
When used in a typed constant or stored in a variable, usually the type is either <code>byte</code> or <code>rune</code> to distinguish character values from integer values.
These are aliases for <code>uint8</code> and <code>int32</code> respectively, but like other integer types in Go, they are distinct and require an explicate cast.
These are aliases for <code>uint8</code> and <code>int32</code> respectively, but like other integer types in Go, they are distinct and require an explicate cast.
<lang go>ch := 'z' // ch is type rune (an int32 type)
<syntaxhighlight lang="go">ch := 'z' // ch is type rune (an int32 type)
var r rune = 'z' // r is type rune
var r rune = 'z' // r is type rune
var b byte = 'z' // b is type byte (an uint8 type)
var b byte = 'z' // b is type byte (an uint8 type)
Line 941: Line 1,076:
r = rune(c)
r = rune(c)
i = int(c)
i = int(c)
b3 := c // equivalent to b</lang>
b3 := c // equivalent to b</syntaxhighlight>


Strings literals are are either interpreted or raw.
Strings literals are are either interpreted or raw.
Line 947: Line 1,082:
Interpreted string literals are contained in double quotes.
Interpreted string literals are contained in double quotes.
They may not contain newlines but may contain backslash escapes.
They may not contain newlines but may contain backslash escapes.
<lang go>str := "z"
<syntaxhighlight lang="go">str := "z"
str = "\u007a"
str = "\u007a"
str = "two\nlines"</lang>
str = "two\nlines"</syntaxhighlight>


This means that 'z' and "z" are different. The former is a character while the latter is a string.
This means that 'z' and "z" are different. The former is a character while the latter is a string.
Line 955: Line 1,090:
Unicode may be included in the string literals.
Unicode may be included in the string literals.
They will be encoded in UTF-8.
They will be encoded in UTF-8.
<lang go>str := "日本語"</lang>
<syntaxhighlight lang="go">str := "日本語"</syntaxhighlight>


Raw string literals are contained within back quotes.
Raw string literals are contained within back quotes.
They may contain any character except a back quote.
They may contain any character except a back quote.
Backslashes have no special meaning.
Backslashes have no special meaning.
<lang go>`\n` == "\\n"</lang>
<syntaxhighlight lang="go">`\n` == "\\n"</syntaxhighlight>


Raw string literals, unlike regular string literals, may also span multiple lines.
Raw string literals, unlike regular string literals, may also span multiple lines.
The newline is included in the string (but not any <code>'\r'</code> characters):
The newline is included in the string (but not any <code>'\r'</code> characters):
<lang go>`abc
<syntaxhighlight lang="go">`abc
def` == "abc\ndef", // never "abc\r\ndef" even if the source file contains CR+LF line endings</lang>
def` == "abc\ndef", // never "abc\r\ndef" even if the source file contains CR+LF line endings</syntaxhighlight>


Go raw string literals serve the purpose of here-strings in other languages.
Go raw string literals serve the purpose of here-strings in other languages.
Line 974: Line 1,109:


In [[Groovy]], unlike in [[Java]], a String literal is delimited with ''single quotes'' (apostrophes(')).
In [[Groovy]], unlike in [[Java]], a String literal is delimited with ''single quotes'' (apostrophes(')).
<lang groovy>def string = 'Able was I'</lang>
<syntaxhighlight lang="groovy">def string = 'Able was I'</syntaxhighlight>


There is a ''double quote'' (quotation mark(")) delimited syntax in Groovy, but it represents an expression construct called a ''GString'' (I know, I know). Inside of a GString, sub-expression substitution of the form ${''subexpression''} may take place. Thus the following results:
There is a ''double quote'' (quotation mark(")) delimited syntax in Groovy, but it represents an expression construct called a ''GString'' (I know, I know). Inside of a GString, sub-expression substitution of the form ${''subexpression''} may take place. Thus the following results:
<lang groovy>def gString = "${string} ere I saw Elba"
<syntaxhighlight lang="groovy">def gString = "${string} ere I saw Elba"


println gString
println gString


//Outputs:
//Outputs:
//Able was I ere I saw Elba</lang>
//Able was I ere I saw Elba</syntaxhighlight>


[[UNIX Shell]] command line users should recognize these forms of syntax as ''strong'' ('-delimited) and ''weak'' ("-delimited) quoting.
[[UNIX Shell]] command line users should recognize these forms of syntax as ''strong'' ('-delimited) and ''weak'' ("-delimited) quoting.
And like [[UNIX Shell]] weak quoting syntax, the evaluated subexpression part of the GString syntax loses its special meaning when preceded by a backslash (\):
And like [[UNIX Shell]] weak quoting syntax, the evaluated subexpression part of the GString syntax loses its special meaning when preceded by a backslash (\):


<lang groovy>def gString2 = "1 + 1 = ${1 + 1}"
<syntaxhighlight lang="groovy">def gString2 = "1 + 1 = ${1 + 1}"
assert gString2 == '1 + 1 = 2'
assert gString2 == '1 + 1 = 2'


def gString3 = "1 + 1 = \${1 + 1}"
def gString3 = "1 + 1 = \${1 + 1}"
assert gString3 == '1 + 1 = ${1 + 1}'</lang>
assert gString3 == '1 + 1 = ${1 + 1}'</syntaxhighlight>


Groovy also supports multi-line String literals and multi-line GString expressions.
Groovy also supports multi-line String literals and multi-line GString expressions.


<lang groovy>def multiLineString = '''
<syntaxhighlight lang="groovy">def multiLineString = '''
A man
A man
A plan
A plan
Line 1,014: Line 1,149:
//A canal:
//A canal:
//Panama!
//Panama!
//</lang>
//</syntaxhighlight>


[[UNIX Shell]] programmers should recognize these forms of syntax as similar in function to the strong and weak forms of ''Here Document'' syntax.
[[UNIX Shell]] programmers should recognize these forms of syntax as similar in function to the strong and weak forms of ''Here Document'' syntax.
Line 1,024: Line 1,159:
However, [[Groovy]] has a special GString syntax that uses slash (/) as a GString delimiter rather that quote ("). In this special syntax, most backslash usages that would require a double backslash in a regular String or GString require only a single backslash (\). This does not create a "regular expression object" (there is not such a thing in [[Groovy]]); however, it does evaluate to form a "regular expression ready" String, as demonstrated in the following:
However, [[Groovy]] has a special GString syntax that uses slash (/) as a GString delimiter rather that quote ("). In this special syntax, most backslash usages that would require a double backslash in a regular String or GString require only a single backslash (\). This does not create a "regular expression object" (there is not such a thing in [[Groovy]]); however, it does evaluate to form a "regular expression ready" String, as demonstrated in the following:


<lang groovy>def regexString = /(\[[Tt]itle\]|\[[Ss]ubject\])${10 * 5}/
<syntaxhighlight lang="groovy">def regexString = /(\[[Tt]itle\]|\[[Ss]ubject\])${10 * 5}/


assert regexString == '(\\[[Tt]itle\\]|\\[[Ss]ubject\\])50'</lang>
assert regexString == '(\\[[Tt]itle\\]|\\[[Ss]ubject\\])50'</syntaxhighlight>


[[Javascript]] users (and others) will recognize the roots of this "regex-ready" syntax as a feature in their own language.
[[Javascript]] users (and others) will recognize the roots of this "regex-ready" syntax as a feature in their own language.
Line 1,032: Line 1,167:
Since apostrophe is used to delimit String literals, that delimiter syntax is not available, as it is in [[Java]], to denote single character literals (type char or Character). However, single character string literals can be converted to character literals by casting. Shown in the examples below are casting using the ''as'' operator, Java-style parenthetical casting, and forced coercion in the intialization of a variable of type char or Character.
Since apostrophe is used to delimit String literals, that delimiter syntax is not available, as it is in [[Java]], to denote single character literals (type char or Character). However, single character string literals can be converted to character literals by casting. Shown in the examples below are casting using the ''as'' operator, Java-style parenthetical casting, and forced coercion in the intialization of a variable of type char or Character.


<lang groovy>assert 'a' instanceof String
<syntaxhighlight lang="groovy">assert 'a' instanceof String
assert ('a' as char) instanceof Character
assert ('a' as char) instanceof Character
assert ((char)'a') instanceof Character
assert ((char)'a') instanceof Character
Line 1,039: Line 1,174:
assert x instanceof Character
assert x instanceof Character
Character y = 'b'
Character y = 'b'
assert y instanceof Character && (x+1 == y)</lang>
assert y instanceof Character && (x+1 == y)</syntaxhighlight>


As in [[Java]], backslash is also used to mask a string delimiter. Thus the following two assignments represent strings containing a single quote and a single apostrophe respectively
As in [[Java]], backslash is also used to mask a string delimiter. Thus the following two assignments represent strings containing a single quote and a single apostrophe respectively


<lang groovy>def quote = "\""
<syntaxhighlight lang="groovy">def quote = "\""
def apostrophe = '\''</lang>
def apostrophe = '\''</syntaxhighlight>


Of course, if you are not using GString subexpression evaluation, you can just use apostrophe delimiters to contain a quote, or quote delimiters to contain an apostrophe.
Of course, if you are not using GString subexpression evaluation, you can just use apostrophe delimiters to contain a quote, or quote delimiters to contain an apostrophe.


<lang groovy>def quote2 = '"'
<syntaxhighlight lang="groovy">def quote2 = '"'
def apostrophe2 = "'"
def apostrophe2 = "'"
assert quote == quote2
assert quote == quote2
assert apostrophe == apostrophe2</lang>
assert apostrophe == apostrophe2</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
Line 1,060: Line 1,195:
Strings may be split across lines, even indented, using the 'gap' syntax:
Strings may be split across lines, even indented, using the 'gap' syntax:


<lang haskell>"abcdef" == "abc\
<syntaxhighlight lang="haskell">"abcdef" == "abc\
\def"
\def"


"abc\ndef" == "abc\n\
"abc\ndef" == "abc\n\
\def"</lang>
\def"</syntaxhighlight>


You can also use <tt>\&amp;</tt> which expands into nothing (but can be useful to interrupt another escape sequence).
You can also use <tt>\&amp;</tt> which expands into nothing (but can be useful to interrupt another escape sequence).
Line 1,074: Line 1,209:
using [http://hackage.haskell.org/package/raw-strings-qq-1.0.2/docs/Text-RawString-QQ.html raw-strings-qq] package:
using [http://hackage.haskell.org/package/raw-strings-qq-1.0.2/docs/Text-RawString-QQ.html raw-strings-qq] package:


<lang haskell>
<syntaxhighlight lang="haskell">
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE QuasiQuotes #-}
import Text.RawString.QQ
import Text.RawString.QQ
Line 1,080: Line 1,215:
"abc\ndef" == [r|abc
"abc\ndef" == [r|abc
def|]
def|]
</syntaxhighlight>
</lang>


=={{header|HicEst}}==
=={{header|HicEst}}==
HicEst makes no distinction between single characters and strings. One can use single quotes, or double quotes, or most non-standard characters.
HicEst makes no distinction between single characters and strings. One can use single quotes, or double quotes, or most non-standard characters.
<lang hicest>CHARACTER c1='A', c2="B", c3=&C&
<syntaxhighlight lang="hicest">CHARACTER c1='A', c2="B", c3=&C&
CHARACTER str1='single quotes', str2="double quotes", str3*100
CHARACTER str1='single quotes', str2="double quotes", str3*100


str3 = % delimit "Nested 'strings' " if needed % </lang>
str3 = % delimit "Nested 'strings' " if needed % </syntaxhighlight>
A null character CHAR(0) is printed as " ", displayed as "." in dialogs, but ends the string in Windows controls such as StatusBar or ClipBoard
A null character CHAR(0) is printed as " ", displayed as "." in dialogs, but ends the string in Windows controls such as StatusBar or ClipBoard
<lang hicest>str3 = 'a string' // CHAR(0) // "may contain" // $CRLF // ~ any character ~ </lang>
<syntaxhighlight lang="hicest">str3 = 'a string' // CHAR(0) // "may contain" // $CRLF // ~ any character ~ </syntaxhighlight>
Named literal constants in HicEst:
Named literal constants in HicEst:
<lang hicest>$TAB == CHAR(9) ! evaluates to 1 (true)
<syntaxhighlight lang="hicest">$TAB == CHAR(9) ! evaluates to 1 (true)
$LF == CHAR(10)
$LF == CHAR(10)
$CR == CHAR(13)
$CR == CHAR(13)
$CRLF == CHAR(13) // CHAR(10) ! concatenation</lang>
$CRLF == CHAR(13) // CHAR(10) ! concatenation</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Below is a little program to demonstrate string literals.
Below is a little program to demonstrate string literals.
<lang Icon>procedure main()
<syntaxhighlight lang="icon">procedure main()


# strings are variable length are not NUL terminated
# strings are variable length are not NUL terminated
Line 1,111: Line 1,246:
every x := c1|s1|s2 do # show them
every x := c1|s1|s2 do # show them
write(" size=",*x,", type=", type(x),", value=", image(x))
write(" size=",*x,", type=", type(x),", value=", image(x))
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,122: Line 1,257:
The single and double quotes are fairly interchangeable allowing one to use whichever isn't to be quoted (though single-quotes seem more well-behaved around integers in strings). Thus the following are both valid character-constant assignments:
The single and double quotes are fairly interchangeable allowing one to use whichever isn't to be quoted (though single-quotes seem more well-behaved around integers in strings). Thus the following are both valid character-constant assignments:


<lang idl>a = " that's a string "
<syntaxhighlight lang="idl">a = " that's a string "
b = ' a "string" is this '</lang>
b = ' a "string" is this '</syntaxhighlight>


In a pinch, a character constant doesn't absolutely have to be terminated, rendering the following valid:
In a pinch, a character constant doesn't absolutely have to be terminated, rendering the following valid:


<lang idl>a = " that's a string</lang>
<syntaxhighlight lang="idl">a = " that's a string</syntaxhighlight>


Duplicating either of them quotes them. Thus the following contains three single quotes and no double-quotes:
Duplicating either of them quotes them. Thus the following contains three single quotes and no double-quotes:


<lang idl>a = ' that''s a string
<syntaxhighlight lang="idl">a = ' that''s a string
print,a
print,a
;==> that's a string</lang>
;==> that's a string</syntaxhighlight>


Things in quotes are not expanded. To get to the content of a variable, leave it unquoted:
Things in quotes are not expanded. To get to the content of a variable, leave it unquoted:


<lang idl>b = 'hello'
<syntaxhighlight lang="idl">b = 'hello'
a = b+' world
a = b+' world
print,a
print,a
;==> hello world</lang>
;==> hello world</syntaxhighlight>


Single-quoted strings of valid hex or octal digits will be expanded if followed by "x" or "o":
Single-quoted strings of valid hex or octal digits will be expanded if followed by "x" or "o":


<lang idl>print,'777'x
<syntaxhighlight lang="idl">print,'777'x
;==> 1911
;==> 1911
print,'777'o
print,'777'o
;==> 511
;==> 511
print,'777'
print,'777'
;==> 777</lang>
;==> 777</syntaxhighlight>


so will be unterminated double-quoted strings if they represent valid octal numbers:
so will be unterminated double-quoted strings if they represent valid octal numbers:


<lang idl>print,"777
<syntaxhighlight lang="idl">print,"777
;==> 511
;==> 511
print,"877
print,"877
;==> 877</lang>
;==> 877</syntaxhighlight>


Note that this renders the following false (common trip-up for IDL newbies):
Note that this renders the following false (common trip-up for IDL newbies):


<lang idl>a = "0"
<syntaxhighlight lang="idl">a = "0"
;==> Syntax error.</lang>
;==> Syntax error.</syntaxhighlight>


...because the number zero indicates that an octal number follows, but the second double-quote is not a valid octal digit.
...because the number zero indicates that an octal number follows, but the second double-quote is not a valid octal digit.
Line 1,167: Line 1,302:
Byte-arrays that are converted into strings are converted to the ascii-characters represented by the bytes. E.g.
Byte-arrays that are converted into strings are converted to the ascii-characters represented by the bytes. E.g.


<lang idl>crlf = string([13b,10b])</lang>
<syntaxhighlight lang="idl">crlf = string([13b,10b])</syntaxhighlight>


=={{header|Inform 7}}==
=={{header|Inform 7}}==
String literals are enclosed in double quotes. These may include raw line breaks, or expressions to be substituted enclosed in square brackets.
String literals are enclosed in double quotes. These may include raw line breaks, or expressions to be substituted enclosed in square brackets.


<lang inform7>Home is a room. The description is "This is where you live...
<syntaxhighlight lang="inform7">Home is a room. The description is "This is where you live...


...with your [number of animals in Home] pet[s]."</lang>
...with your [number of animals in Home] pet[s]."</syntaxhighlight>


Single quotes in a string are translated to double quotes when they occur outside of a word: the string literal <lang inform7>"'That's nice,' said the captain."</lang>
Single quotes in a string are translated to double quotes when they occur outside of a word: the string literal <syntaxhighlight lang="inform7">"'That's nice,' said the captain."</syntaxhighlight>
will print as
will print as
<pre>"That's nice," said the captain.</pre>
<pre>"That's nice," said the captain.</pre>
Line 1,182: Line 1,317:
Raw linebreak must be double -- single linebreaks will be collapsed unless explicitly marked with `[line break]`. In addition, leading whitespace is stripped from each line. This:
Raw linebreak must be double -- single linebreaks will be collapsed unless explicitly marked with `[line break]`. In addition, leading whitespace is stripped from each line. This:


<lang inform7>"
<syntaxhighlight lang="inform7">"
\
\
\
\
\
\
\"</lang>
\"</syntaxhighlight>
will print as:
will print as:
<pre>\\\\
<pre>\\\\
Line 1,192: Line 1,327:


while this:
while this:
<lang inform7>"
<syntaxhighlight lang="inform7">"
[line break]\
[line break]\
[line break] \
[line break] \
[line break] \
[line break] \
[line break] \"</lang>
[line break] \"</syntaxhighlight>
will insert line breaks and preserve the following whitespace, printing as:
will insert line breaks and preserve the following whitespace, printing as:
<pre>
<pre>
Line 1,213: Line 1,348:
Examples:
Examples:


<lang j>'x' NB. Scalar character
<syntaxhighlight lang="j">'x' NB. Scalar character
'string' NB. List of characters, i.e. a string
'string' NB. List of characters, i.e. a string
'can''t get simpler' NB. Embedded single-quote</lang>
'can''t get simpler' NB. Embedded single-quote</syntaxhighlight>


Like VB, J can include newlines and other special characters in literals with concatentation. Also like VB, J comes with certain constants predefined for some characters:
Like VB, J can include newlines and other special characters in literals with concatentation. Also like VB, J comes with certain constants predefined for some characters:


<lang j>'Here is line 1',LF,'and line two'
<syntaxhighlight lang="j">'Here is line 1',LF,'and line two'


'On a mac, you need',CR,'a carriage return'
'On a mac, you need',CR,'a carriage return'
Line 1,225: Line 1,360:
'And on windows, ',CRLF,'you need both'
'And on windows, ',CRLF,'you need both'


TAB,TAB,TAB,'Everyone loves tabs!'</lang>
TAB,TAB,TAB,'Everyone loves tabs!'</syntaxhighlight>


These constants are simply names assigned to selections from the ASCII alphabet. That is, the standard library executes lines like this:
These constants are simply names assigned to selections from the ASCII alphabet. That is, the standard library executes lines like this:


<lang j>CR =: 13 { a.
<syntaxhighlight lang="j">CR =: 13 { a.
LF =: 10 { a.
LF =: 10 { a.
CRLF =: CR,LF NB. Or just 10 13 { a.
CRLF =: CR,LF NB. Or just 10 13 { a.
TAB =: 9 { a.</lang>
TAB =: 9 { a.</syntaxhighlight>


Since these constants are nothing special, it can be seen that any variable can be similarly included in a literal:
Since these constants are nothing special, it can be seen that any variable can be similarly included in a literal:


<lang j>NAME =: 'John Q. Public'
<syntaxhighlight lang="j">NAME =: 'John Q. Public'
'Hello, ',NAME,' you may have already won $1,000,000'</lang>
'Hello, ',NAME,' you may have already won $1,000,000'</syntaxhighlight>


For multiline literals, you may define an explicit noun, which is terminated by a lone <code>)</code>
For multiline literals, you may define an explicit noun, which is terminated by a lone <code>)</code>


<lang j>template =: noun define
<syntaxhighlight lang="j">template =: noun define
Hello, NAME.
Hello, NAME.


Line 1,249: Line 1,384:
To collect your winnings, please send $PAYMENT
To collect your winnings, please send $PAYMENT
to ADDRESS.
to ADDRESS.
)</lang>
)</syntaxhighlight>


Simple substitution is most easily effected by using loading a standard script:
Simple substitution is most easily effected by using loading a standard script:


<lang j>load 'strings'
<syntaxhighlight lang="j">load 'strings'


name =: 'John Q. Public'
name =: 'John Q. Public'
Line 1,264: Line 1,399:
sources =: ":&.> name;shyster;amount;payment;address
sources =: ":&.> name;shyster;amount;payment;address


message =: template rplc targets,.sources</lang>
message =: template rplc targets,.sources</syntaxhighlight>
While C-like interpolation can be effected with another:
While C-like interpolation can be effected with another:


<lang j> load 'printf'
<syntaxhighlight lang="j"> load 'printf'
'This should look %d%% familiar \nto programmers of %s.' sprintf 99;'C'
'This should look %d%% familiar \nto programmers of %s.' sprintf 99;'C'
This should look 99% familiar
This should look 99% familiar
to programmers of C.</lang>
to programmers of C.</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==


<lang java> char a = 'a'; // prints as: a
<syntaxhighlight lang="java"> char a = 'a'; // prints as: a
String b = "abc"; // prints as: abc
String b = "abc"; // prints as: abc
char doubleQuote = '"'; // prints as: "
char doubleQuote = '"'; // prints as: "
char singleQuote = '\''; // prints as: '
char singleQuote = '\''; // prints as: '
String singleQuotes = "''"; // prints as: ''
String singleQuotes = "''"; // prints as: ''
String doubleQuotes = "\"\""; // prints as: ""</lang>
String doubleQuotes = "\"\""; // prints as: ""</syntaxhighlight>


Null characters ('\0') are printed as spaces in Java. They will not terminate a String as they would in C or C++. So, the String "this \0is \0a \0test" will print like this:
Null characters ('\0') are printed as spaces in Java. They will not terminate a String as they would in C or C++. So, the String "this \0is \0a \0test" will print like this:
Line 1,293: Line 1,428:
Unicode characters can be entered as literals or as 4 character hexadecimal escapes. The following expressions are equivalent:
Unicode characters can be entered as literals or as 4 character hexadecimal escapes. The following expressions are equivalent:


<lang JavaScript>(function () {
<syntaxhighlight lang="javascript">(function () {
return "αβγδ 中间来点中文 🐫 אבגד"
return "αβγδ 中间来点中文 🐫 אבגד"
})();
})();
Line 1,300: Line 1,435:
(function() {
(function() {
return "\u03b1\u03b2\u03b3\u03b4 \u4e2d\u95f4\u6765\u70b9\u4e2d\u6587 \ud83d\udc2b \u05d0\u05d1\u05d2\u05d3";
return "\u03b1\u03b2\u03b3\u03b4 \u4e2d\u95f4\u6765\u70b9\u4e2d\u6587 \ud83d\udc2b \u05d0\u05d1\u05d2\u05d3";
})();</lang>
})();</syntaxhighlight>


Note that in the case of the Emoji character above, where more than 4 hexadecimal characters are needed, ES5 requires us to separately write a pair of surrogate halves, and the '''String.length''' of such characters is 2.
Note that in the case of the Emoji character above, where more than 4 hexadecimal characters are needed, ES5 requires us to separately write a pair of surrogate halves, and the '''String.length''' of such characters is 2.
Line 1,309: Line 1,444:
ES6 also introduces template literals, which are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. Template literals are enclosed by the backtick (<code>` `</code>) (grave accent) character instead of double or single quotes.
ES6 also introduces template literals, which are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. Template literals are enclosed by the backtick (<code>` `</code>) (grave accent) character instead of double or single quotes.


<lang JavaScript>const multiLine = `string text line 1
<syntaxhighlight lang="javascript">const multiLine = `string text line 1
string text line 2`
string text line 2`
const expression = `expressions are also supported, using \$\{\}: ${multiLine}`
const expression = `expressions are also supported, using \$\{\}: ${multiLine}`


console.log(expression)</lang>
console.log(expression)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,337: Line 1,472:
=={{header|Julia}}==
=={{header|Julia}}==
Concatenation:
Concatenation:
<lang julia>greet = "Hello"
<syntaxhighlight lang="julia">greet = "Hello"
whom = "world"
whom = "world"
greet * ", " * whom * "."</lang>
greet * ", " * whom * "."</syntaxhighlight>


Interpolation:
Interpolation:
<lang julia>"$greet, $whom."</lang>
<syntaxhighlight lang="julia">"$greet, $whom."</syntaxhighlight>


Both will output:
Both will output:
<lang julia>Hello, world.</lang>
<syntaxhighlight lang="julia">Hello, world.</syntaxhighlight>


Triple-quoted strings
Triple-quoted strings
<lang julia>str = """Hello,
<syntaxhighlight lang="julia">str = """Hello,
world.
world.
"""
"""


print(str)</lang>
print(str)</syntaxhighlight>


Will output:
Will output:


<lang julia>Hello,
<syntaxhighlight lang="julia">Hello,
world.</lang>
world.</syntaxhighlight>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
Line 1,370: Line 1,505:


Here are some examples of these :
Here are some examples of these :
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


fun main(args: Array<String>) {
fun main(args: Array<String>) {
Line 1,395: Line 1,530:
println(rsl)
println(rsl)
println(msl)
println(msl)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,425: Line 1,560:


===Quoted Strings===
===Quoted Strings===
<lang Lasso>'I\'m a 2" string\n'
<syntaxhighlight lang="lasso">'I\'m a 2" string\n'
"I'm a 2\" string\n"</lang>
"I'm a 2\" string\n"</syntaxhighlight>


===Ticked Strings===
===Ticked Strings===
In the below example here \n would not be a line feed, it represents a backslash and n.
In the below example here \n would not be a line feed, it represents a backslash and n.
<lang Lasso>`I'm also a 2" string\n`</lang>
<syntaxhighlight lang="lasso">`I'm also a 2" string\n`</syntaxhighlight>


=={{header|LaTeX}}==
=={{header|LaTeX}}==
Line 1,440: Line 1,575:
For example, to typeset 'a' is for "apple" in LaTeX, one would type
For example, to typeset 'a' is for "apple" in LaTeX, one would type


<syntaxhighlight lang="latex">\documentclass{minimal}
<lang latex>`a' is for ``apple'''''''</lang>
\begin{document}
`a' is for ``apple"
\end{document}</syntaxhighlight>


One common mistake is to use the same symbol for opening and closing quotes,
One common mistake is to use the same symbol for opening and closing quotes,
Line 1,448: Line 1,586:


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
'Liberty BASIC does not support escape characters within literal strings.
'Liberty BASIC does not support escape characters within literal strings.
print "Quotation mark:"
print "Quotation mark:"
Line 1,458: Line 1,596:
'Print literal string displaying quotation marks.
'Print literal string displaying quotation marks.
print chr$(34) + "Hello, World." + chr$(34)
print chr$(34) + "Hello, World." + chr$(34)
</syntaxhighlight>
</lang>


=={{header|Lingo}}==
=={{header|Lingo}}==
* Lingo only supports single quotes for string literals. Single quotes inside string literals have to be replaced by "&QUOTE&":
* Lingo only supports single quotes for string literals. Single quotes inside string literals have to be replaced by "&QUOTE&":
<lang lingo>str = "Hello "&QUOTE&"world!"&QUOTE
<syntaxhighlight lang="lingo">str = "Hello "&QUOTE&"world!"&QUOTE
put str
put str
-- "Hello "world!""</lang>
-- "Hello "world!""</syntaxhighlight>


* Lingo does not support heredoc syntax, but only multiline string literals by using the line continuation character "\":
* Lingo does not support heredoc syntax, but only multiline string literals by using the line continuation character "\":
<lang lingo>str = "This is the first line.\
<syntaxhighlight lang="lingo">str = "This is the first line.\
This is the second line.\
This is the second line.\
This is the third line."</lang>
This is the third line."</syntaxhighlight>


* Lingo does not support automatic variable expansion in strings. But the function value() can be used to expand template strings in the current context:
* Lingo does not support automatic variable expansion in strings. But the function value() can be used to expand template strings in the current context:
<lang lingo>template = QUOTE&"Milliseconds since last reboot: "&QUOTE&"&_system.milliseconds"
<syntaxhighlight lang="lingo">template = QUOTE&"Milliseconds since last reboot: "&QUOTE&"&_system.milliseconds"


-- expand template in current context
-- expand template in current context
str = value(template)
str = value(template)
put str
put str
-- "Milliseconds since last reboot: 20077664"</lang>
-- "Milliseconds since last reboot: 20077664"</syntaxhighlight>


=={{header|Lisaac}}==
=={{header|Lisaac}}==
Characters:
Characters:
<lang Lisaac>c1 := 'a';
<syntaxhighlight lang="lisaac">c1 := 'a';
c2 := '\n'; // newline
c2 := '\n'; // newline
c3 := '\''; // quote
c3 := '\''; // quote
Line 1,487: Line 1,625:
c5 := '\10\'; // decimal
c5 := '\10\'; // decimal
c6 := '\0Ah\'; // hexadecimal
c6 := '\0Ah\'; // hexadecimal
c7 := '\10010110b\'; // binary</lang>
c7 := '\10010110b\'; // binary</syntaxhighlight>
Strings:
Strings:
<lang Lisaac>s1 := "this is a\nsample"; // newline
<syntaxhighlight lang="lisaac">s1 := "this is a\nsample"; // newline
s2 := "\""; // double quote
s2 := "\""; // double quote
s3 := "abc\
s3 := "abc\
\xyz"; // "abcxyz", cut the gap</lang>
\xyz"; // "abcxyz", cut the gap</syntaxhighlight>


=={{header|LiveCode}}==
=={{header|LiveCode}}==
LiveCode has only one string representation using quotes. Characters are accessed through chunk expressions, specifically char. Some special characters are built-in constants such as quote, space, comma, cr, return. There is no support for escaping characters or multiline literals.<lang LiveCode>put "Literal string" -- Literal string
LiveCode has only one string representation using quotes. Characters are accessed through chunk expressions, specifically char. Some special characters are built-in constants such as quote, space, comma, cr, return. There is no support for escaping characters or multiline literals.<syntaxhighlight lang="livecode">put "Literal string" -- Literal string
put char 1 of "Literal string" -- L
put char 1 of "Literal string" -- L
put char 1 to 7 of "Literal string" -- Literal
put char 1 to 7 of "Literal string" -- Literal
put word 1 of "Literal string" -- Literal
put word 1 of "Literal string" -- Literal
put quote & "string" & quote -- "string"</lang>
put quote & "string" & quote -- "string"</syntaxhighlight>


=={{header|Logo}}==
=={{header|Logo}}==
Logo does not have a string or character type that is separate from its symbol type ("word"). A literal word is specified by prefixing a double-quote character.
Logo does not have a string or character type that is separate from its symbol type ("word"). A literal word is specified by prefixing a double-quote character.
Reserved and delimiting characters, ()[];~+-*/\=<>| and newline, may be used if preceded by a backslash. Alternatively, the string may be wrapped in vertical bars, in which case only backslash and vertical bar need be escaped.
Reserved and delimiting characters, ()[];~+-*/\=<>| and newline, may be used if preceded by a backslash. Alternatively, the string may be wrapped in vertical bars, in which case only backslash and vertical bar need be escaped.
<lang logo>print "Hello\,\ world
<syntaxhighlight lang="logo">print "Hello\,\ world
print "|Hello, world|</lang>
print "|Hello, world|</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
Line 1,513: Line 1,651:
to be embedded within a string enclosed with the other symbol.
to be embedded within a string enclosed with the other symbol.


<lang lua>singlequotestring = 'can contain "double quotes"'
<syntaxhighlight lang="lua">singlequotestring = 'can contain "double quotes"'
doublequotestring = "can contain 'single quotes'"
doublequotestring = "can contain 'single quotes'"
longstring = [[can contain
longstring = [[can contain
newlines]]
newlines]]
longstring2 = [==[ can contain [[ other ]=] longstring " and ' string [===[ qualifiers]==]</lang>
longstring2 = [==[ can contain [[ other ]=] longstring " and ' string [===[ qualifiers]==]</syntaxhighlight>


Note that interpolation of variables names within a string does not take place.
Note that interpolation of variables names within a string does not take place.
Line 1,524: Line 1,662:


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Print "Hello {World}"
Print "Hello {World}"
Print {Hello "World"}
Print {Hello "World"}
Line 1,532: Line 1,670:
Print """Hello There"""={"Hello There"}
Print """Hello There"""={"Hello There"}
Print Quote$("Hello There")={"Hello There"}
Print Quote$("Hello There")={"Hello There"}
</syntaxhighlight>
</lang>


=={{header|M4}}==
=={{header|M4}}==
The quoting characters are <tt>`</tt> and <tt>'</tt>,
The quoting characters are <tt>`</tt> and <tt>'</tt>,
but can be changed by the <code>changequote</code> macro:
but can be changed by the <code>changequote</code> macro:
<lang m4>`this is quoted string'</lang>
<syntaxhighlight lang="m4">`this is quoted string'</syntaxhighlight>
<lang m4>changequote(`[',`]')dnl
<syntaxhighlight lang="m4">changequote(`[',`]')dnl
[this is a quoted string]</lang>
[this is a quoted string]</syntaxhighlight>


=={{header|Maple}}==
=={{header|Maple}}==
There is no separate character type in Maple; a character is just a string of length equal to 1.
There is no separate character type in Maple; a character is just a string of length equal to 1.
<syntaxhighlight lang="maple">
<lang Maple>
> "foobar";
> "foobar";
"foobar"
"foobar"
Line 1,553: Line 1,691:
> "c"; # a character
> "c"; # a character
"c"
"c"
</syntaxhighlight>
</lang>
Note that adjacent strings in the input (separated only by white-space) are concatenated automatically by the parser.
Note that adjacent strings in the input (separated only by white-space) are concatenated automatically by the parser.
<syntaxhighlight lang="maple">
<lang Maple>
> "foo" "bar";
> "foo" "bar";
"foobar"
"foobar"
</syntaxhighlight>
</lang>
Since variable names are not distinguished lexically from other text (such as by using a "$" prefix, as in some shells), Maple does not do any kind of variable expansion inside strings.
Since variable names are not distinguished lexically from other text (such as by using a "$" prefix, as in some shells), Maple does not do any kind of variable expansion inside strings.


=={{header|Mathematica}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==


<lang Mathematica>There is no character type in Mathematica, only string type.
<syntaxhighlight lang="mathematica">There is no character type in Mathematica, only string type.
"c"; // String (result: "c")
"c"; // String (result: "c")
"\n"; // String (result: newline character)</lang>
"\n"; // String (result: newline character)</syntaxhighlight>


=={{header|MATLAB}}==
=={{header|MATLAB}}==
Strings start and end with single quotes, the escape sequence for a single quote with in a string, is the use of two consequtive single quotes
Strings start and end with single quotes, the escape sequence for a single quote with in a string, is the use of two consequtive single quotes
<syntaxhighlight lang="matlab">
<lang Matlab>
s1 = 'abcd' % simple string
s1 = 'abcd' % simple string
s2 = 'ab''cd' % string containing a single quote
s2 = 'ab''cd' % string containing a single quote
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,582: Line 1,720:


=={{header|Maxima}}==
=={{header|Maxima}}==
<lang maxima>/* A string */
<syntaxhighlight lang="maxima">/* A string */
"The quick brown fox jumps over the lazy dog";
"The quick brown fox jumps over the lazy dog";


/* A character - just a one character string */
/* A character - just a one character string */
"a"</lang>
"a"</syntaxhighlight>


=={{header|Metafont}}==
=={{header|Metafont}}==
Line 1,592: Line 1,730:
In Metafont there's no difference between a single character string and a single character. Moreover, the double quotes (which delimites a string) cannot be inserted directly into a string; for this reason, the basic Metafont macro set defines
In Metafont there's no difference between a single character string and a single character. Moreover, the double quotes (which delimites a string) cannot be inserted directly into a string; for this reason, the basic Metafont macro set defines


<lang metafont>string ditto; ditto = char 34;</lang>
<syntaxhighlight lang="metafont">string ditto; ditto = char 34;</syntaxhighlight>


i.e. a string which is the single character having ASCII code 34 ("). Macro or variables expansion inside a string block is inhibited.
i.e. a string which is the single character having ASCII code 34 ("). Macro or variables expansion inside a string block is inhibited.


<lang metafont>message "You've said: " & ditto & "Good bye!" & ditto & ".";</lang>
<syntaxhighlight lang="metafont">message "You've said: " & ditto & "Good bye!" & ditto & ".";</syntaxhighlight>


=={{header|ML/I}}==
=={{header|ML/I}}==
Line 1,602: Line 1,740:
ML/I treats all input and programs as character streams. Strings do not have to be quoted; they are taken 'as is'. If one wishes to ensure that a string is taken literally (i.e. not evaluated), it is enclosed in ''literal brackets''. There are no predefined literal brackets; the programmer can define anything suitable, usually by setting up a ''matched text skip'', using the MCSKIP operation macro. By convention, the pair <> is used for literal brackets, unless this clashes in the case of a particular processing task.
ML/I treats all input and programs as character streams. Strings do not have to be quoted; they are taken 'as is'. If one wishes to ensure that a string is taken literally (i.e. not evaluated), it is enclosed in ''literal brackets''. There are no predefined literal brackets; the programmer can define anything suitable, usually by setting up a ''matched text skip'', using the MCSKIP operation macro. By convention, the pair <> is used for literal brackets, unless this clashes in the case of a particular processing task.
===Input===
===Input===
<lang ML/I>MCSKIP "WITH" NL
<syntaxhighlight lang="ml/i">MCSKIP "WITH" NL
"" Literals/String
"" Literals/String
MCINS %.
MCINS %.
Line 1,612: Line 1,750:
"" evaluated.
"" evaluated.
This is the first mention of Bob
This is the first mention of Bob
<and here we mention Bob again></lang>
<and here we mention Bob again></syntaxhighlight>


===Output===
===Output===
<lang ML/I>This is the first mention of Alice
<syntaxhighlight lang="ml/i">This is the first mention of Alice
and here we mention Bob again</lang>
and here we mention Bob again</syntaxhighlight>

=={{header|MIPS Assembly}}==
{{works with|https://github.com/Kingcom/armips ARMIPS}}
Strings are specified using single or double quotes. The assembler will convert each letter of the string into its ASCII equivalent during the assembly process. Therefore, all of the following statements have the same effect:

<syntaxhighlight lang="mips">li a0,'A'
li a0,0x41
li a0,65
li a0,0b01000001</syntaxhighlight>

This means that you can do compile-time "character addition/subtraction" and the like, to better communicate <i>why</i> your code is doing what it's doing.
<syntaxhighlight lang="mips">;print 0 if $t0 if even, 1 if $t0 is odd

andi t0,t0,1 ;clear all but bit 1. This tells us if $t0 is odd or even.
addiu t0,"0" ;add ASCII 0 (0x30) to $t0
jal PrintChar ;implementation-defined print routine that prints the ASCII value of $t0 to the screen.</syntaxhighlight>

ASCII strings use <code>.byte</code> for declaration. Control codes are implemented by strategically placing commas and their numeric values <i>outside of quotation marks,</i> like so:

<syntaxhighlight lang="mips">MyString:
.byte "Hello World!",13,10,0 ;carriage return, line feed, null terminator
.align 4 ;pads to the next 4 byte-boundary</syntaxhighlight>

As with most RISC CPUs, alignment is a must, especially when working with ASCII strings. ARMIPS doesn't provide alignment automatically, but it does have the <code>.align</code> directive to provide sufficient padding (if necessary) to ensure everything after your string is properly aligned. If it was already aligned, the directive will do nothing rather than burn the bytes, meaning that you don't have to take the time to count how long your string is. There's no memory wasted by dropping a <code>.align</code> after every piece of byte-length data, so might as well.



=={{header|Modula-3}}==
=={{header|Modula-3}}==
Characters in Modula-3 use single quotes.
Characters in Modula-3 use single quotes.
<lang modula3>VAR char: CHAR := 'a';</lang>
<syntaxhighlight lang="modula3">VAR char: CHAR := 'a';</syntaxhighlight>
Strings in Modula-3 use double quotes.
Strings in Modula-3 use double quotes.
<lang modula3>VAR str: TEXT := "foo";</lang>
<syntaxhighlight lang="modula3">VAR str: TEXT := "foo";</syntaxhighlight>
<code>TEXT</code> is the string type in Modula-3.
<code>TEXT</code> is the string type in Modula-3.
Characters can be stored in an array and then converted to type TEXT using the function <code>Text.FromChars</code> in the <code>Text</code> module.
Characters can be stored in an array and then converted to type TEXT using the function <code>Text.FromChars</code> in the <code>Text</code> module.


Strings (of type <code>TEXT</code>) can be converted into an array of characters using the function <code>Text.SetChars</code>.
Strings (of type <code>TEXT</code>) can be converted into an array of characters using the function <code>Text.SetChars</code>.
<lang modula3>VAR str: TEXT := "Foo";
<syntaxhighlight lang="modula3">VAR str: TEXT := "Foo";
VAR chrarray: ARRAY [1..3] OF CHAR;
VAR chrarray: ARRAY [1..3] OF CHAR;


Text.SetChars(chrarray, str);
Text.SetChars(chrarray, str);
(* chrarray now has the value ['F', 'o', 'o'] *)</lang>
(* chrarray now has the value ['F', 'o', 'o'] *)</syntaxhighlight>


=={{header|MUMPS}}==
=={{header|MUMPS}}==
Line 1,657: Line 1,820:
Nemerle also has a recursive string literal, enclosed within <# #>, that is the same as a literal string, except that it allows nesting of strings.
Nemerle also has a recursive string literal, enclosed within <# #>, that is the same as a literal string, except that it allows nesting of strings.


<lang Nemerle>'a' // character literal
<syntaxhighlight lang="nemerle">'a' // character literal
'\n' // also a character literal
'\n' // also a character literal
"foo\nbar" // string literal
"foo\nbar" // string literal
Line 1,668: Line 1,831:
like "\n".#> // same as "This string type can contain any symbols including \"\nand new lines. "
like "\n".#> // same as "This string type can contain any symbols including \"\nand new lines. "
// + "It does not\nsupport escape codes\nlike \"\\n\"."
// + "It does not\nsupport escape codes\nlike \"\\n\"."
<#Test <# Inner #> end#> // same as "Test <# Inner #> end" (i.e. this string type support recursion.</lang>
<#Test <# Inner #> end#> // same as "Test <# Inner #> end" (i.e. this string type support recursion.</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>var c = 'c'
<syntaxhighlight lang="nim">var c = 'c'
var s = "foobar"
var s = "foobar"
var l = """foobar
var l = """foobar
Line 1,677: Line 1,840:
more test here"""
more test here"""


var f = r"C:\texts\text.txt" # Raw string</lang>
var f = r"C:\texts\text.txt" # Raw string</syntaxhighlight>


=={{header|OASYS Assembler}}==
=={{header|OASYS Assembler}}==
Line 1,694: Line 1,857:
=={{header|Objective-C}}==
=={{header|Objective-C}}==
The same as C, with the addition of the new string literal
The same as C, with the addition of the new string literal
<lang objc>@"Hello, world!"</lang>
<syntaxhighlight lang="objc">@"Hello, world!"</syntaxhighlight>
which represents a pointer to a statically allocated string object, of type <tt>NSString *</tt>, similar to string literals in Java. You can use this literal like other object pointers, e.g. call methods on it <code>[@"Hello, world!" uppercaseString]</code>.
which represents a pointer to a statically allocated string object, of type <tt>NSString *</tt>, similar to string literals in Java. You can use this literal like other object pointers, e.g. call methods on it <code>[@"Hello, world!" uppercaseString]</code>.


Line 1,700: Line 1,863:


Characters are contained in single quotes:
Characters are contained in single quotes:
<lang ocaml># 'a';;
<syntaxhighlight lang="ocaml"># 'a';;
- : char = 'a'</lang>
- : char = 'a'</syntaxhighlight>


Strings are contained in double quotes:
Strings are contained in double quotes:
<lang ocaml># "Hello world";;
<syntaxhighlight lang="ocaml"># "Hello world";;
- : string = "Hello world"</lang>
- : string = "Hello world"</syntaxhighlight>


Strings may be split across lines and concatenated using the following syntax: (the newline and any blanks at the beginning of the second line is ignored)
Strings may be split across lines and concatenated using the following syntax: (the newline and any blanks at the beginning of the second line is ignored)
<lang ocaml># "abc\
<syntaxhighlight lang="ocaml"># "abc\
def";;
def";;
- : string = "abcdef"</lang>
- : string = "abcdef"</syntaxhighlight>


If the above syntax is not used then any newlines and whitespace are included in the string:
If the above syntax is not used then any newlines and whitespace are included in the string:
<lang ocaml># "abc
<syntaxhighlight lang="ocaml"># "abc
def";;
def";;
- : string = "abc\n def"</lang>
- : string = "abc\n def"</syntaxhighlight>


Another syntax to include verbatim text:
Another syntax to include verbatim text:
<lang ocaml># {id|
<syntaxhighlight lang="ocaml"># {id|
Hello World!
Hello World!
|id} ;;
|id} ;;
- : string = "\n Hello World!\n"</lang>
- : string = "\n Hello World!\n"</syntaxhighlight>


=={{header|Octave}}==
=={{header|Octave}}==
Line 1,727: Line 1,890:
In order to maintain compatible with Matlab,
In order to maintain compatible with Matlab,
it is recommended to use single quotes for defining strings.
it is recommended to use single quotes for defining strings.
<syntaxhighlight lang="octave">
<lang Octave>
s1 = 'abcd' % simple string
s1 = 'abcd' % simple string
s2 = 'ab''cd' % string containing a single quote using an escaped single quote
s2 = 'ab''cd' % string containing a single quote using an escaped single quote
Line 1,733: Line 1,896:
s4 = "ab'cd" % string containing a single quote
s4 = "ab'cd" % string containing a single quote
s5 = "ab""cd" % string containing a double quote using an escaped double quote
s5 = "ab""cd" % string containing a double quote using an escaped double quote
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,754: Line 1,917:
There is no character type : characters are integers representing unicode value of the character.
There is no character type : characters are integers representing unicode value of the character.


<syntaxhighlight lang="oforth">'a'
<lang Oforth>'a'
'\''
'\''
"abcd"
"abcd"
"ab\ncd"
"ab\ncd"
"ab\" and \" cd"</lang>
"ab\" and \" cd"</syntaxhighlight>


=={{header|Oz}}==
=={{header|Oz}}==
<lang oz>declare
<syntaxhighlight lang="oz">declare
Digit0 = &0 %% the character '0'
Digit0 = &0 %% the character '0'
NewLine = &\n %% a character with special representation
NewLine = &\n %% a character with special representation
Line 1,775: Line 1,938:
MyName = "Peter"
MyName = "Peter"
MyAge = 8
MyAge = 8
{System.showInfo MyName # " is " # MyAge # " years old."}</lang>
{System.showInfo MyName # " is " # MyAge # " years old."}</syntaxhighlight>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
Line 1,798: Line 1,961:
Double-quotes allows you to interpolate variables and escape sequences, while single-quotes do not.
Double-quotes allows you to interpolate variables and escape sequences, while single-quotes do not.


<lang perl>'c'; # character
<syntaxhighlight lang="perl">'c'; # character
'hello'; # these two strings are the same
'hello'; # these two strings are the same
"hello";
"hello";
Line 1,818: Line 1,981:
<<'END'; # Here-Document like single-quoted
<<'END'; # Here-Document like single-quoted
Same as above, but no interpolation of $variables.
Same as above, but no interpolation of $variables.
END</lang>
END</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
{{libheader|Phix/basics}}
single character literals (incidentally entirely equivalient to their ascii value) require single quotes, eg
single character literals (incidentally entirely equivalient to their ascii value) require single quotes, eg

<lang Phix>constant UPPERCASEJ = 'J' -- equivalent to 74</lang>
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">UPPERCASEJ</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'J'</span> <span style="color: #000080;font-style:italic;">-- equivalent to 74</span>
<!--</syntaxhighlight>-->

string literals use double quotes, eg
string literals use double quotes, eg

<lang Phix>constant hw = "Hello World!",
<!--<syntaxhighlight lang="phix">-->
mt = "" -- empty string</lang>
<span style="color: #008080;">constant</span> <span style="color: #000000;">hw</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Hello World!"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">mt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> <span style="color: #000080;font-style:italic;">-- empty string</span>
<!--</syntaxhighlight>-->

Note that 'z' and "z" are quite different. In Phix there is a strong difference between a character and a string.
Note that 'z' and "z" are quite different. In Phix there is a strong difference between a character and a string.


All strings are ansi or utf8, depending on the encoding of the source file, eg
All strings are ansi or utf8, depending on the encoding of the source file, eg
<lang Phix>s = "日本語"</lang>
<syntaxhighlight lang="phix">s = "日本語"</syntaxhighlight>
Utf8 strings are byte-subscripted rather than character-subscripted, so s[3] is not necessarily the third character.<br>
Utf8 strings are byte-subscripted rather than character-subscripted, so s[3] is not necessarily the third character.<br>
Phix strings have a length field in the (internal) header, /and/ a terminating null, so they can be used directly when interfacing to C-style languages.<br>
Phix strings have a length field in the (internal) header, /and/ a terminating null, so they can be used directly when interfacing to C-style languages.<br>
Phix strings can also be used to hold "raw binary", ie instead of a sequence of characters, a sequence of any bytes in the range 0 to 255.<br>
Phix strings can also be used to hold "raw binary", ie instead of a sequence of characters, a sequence of any bytes in the range 0 to 255.<br>
Strings are fully mutable: you can append, prepend, replace, substitute, and crop characters and slices (/substrings) any way you like, eg
Strings are fully mutable: you can append, prepend, replace, substitute, and crop characters and slices (/substrings) any way you like, eg

<lang Phix>string s = "food"
<!--<syntaxhighlight lang="phix">-->
s[2..3] = 'e' -- s is now "feed" (replace all)
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"food"</span>
s[2..2] = "east" -- s is now "feasted" (replace substring)
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'e'</span> <span style="color: #000080;font-style:italic;">-- s is now "feed" (replace all)</span>
s[2..5] = "" -- s is now "fed"</lang>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"east"</span> <span style="color: #000080;font-style:italic;">-- s is now "feasted" (replace substring)</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">5</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> <span style="color: #000080;font-style:italic;">-- s is now "fed"</span>
<!--</syntaxhighlight>-->

Special characters may be entered (between quotes) using a back-slash:
Special characters may be entered (between quotes) using a back-slash:
<pre>
<pre>
Line 1,858: Line 2,034:
Strings can also be entered by using triple quotes or backticks intead of double quotes to include linebreaks and avoid any backslash interpretation.
Strings can also be entered by using triple quotes or backticks intead of double quotes to include linebreaks and avoid any backslash interpretation.
If the literal begins with a newline, it is discarded and any immediately following leading underscores specify a (maximum) trimming that should be applied to all subsequent lines. Examples:
If the literal begins with a newline, it is discarded and any immediately following leading underscores specify a (maximum) trimming that should be applied to all subsequent lines. Examples:
<lang Phix>ts = """
this
string\thing"""


<!--<syntaxhighlight lang="phix">-->
ts = """
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> ""<span style="color: #008000;">`
_____this
this
string\thing"""
string\thing`</span>""
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> ""<span style="color: #008000;">`
_____this
string\thing`</span>""
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">`this
string\thing`</span>
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"this\nstring\\thing"</span>
<!--</syntaxhighlight>-->


ts = `this
string\thing`

ts = "this\nstring\\thing"</lang>
which are all equivalent.
which are all equivalent.


Line 1,877: Line 2,057:


Hex string literals are also supported (mainly for compatibility with OpenEuphoria, x/u/U for 1/2/4 byte codes), eg:
Hex string literals are also supported (mainly for compatibility with OpenEuphoria, x/u/U for 1/2/4 byte codes), eg:

<lang Phix>?x"68 65 6c 6c 6f"; -- displays "hello"</lang>
<!--<syntaxhighlight lang="phix">-->
<span style="color: #0000FF;">?</span><span style="color: #000000;">x</span><span style="color: #008000;">"68 65 6c 6c 6f"</span><span style="color: #0000FF;">;</span> <span style="color: #000080;font-style:italic;">-- displays "hello"</span>
<!--</syntaxhighlight>-->


=={{header|PHP}}==
=={{header|PHP}}==
Line 1,886: Line 2,069:
while single-quotes do not.
while single-quotes do not.


<lang php>'c'; # character
<syntaxhighlight lang="php">'c'; # character
'hello'; # these two strings are the same
'hello'; # these two strings are the same
"hello";
"hello";
Line 1,901: Line 2,084:
<<'END' # Here-Document like single-quoted
<<'END' # Here-Document like single-quoted
Same as above, but no interpolation of $variables.
Same as above, but no interpolation of $variables.
END;</lang>
END;</syntaxhighlight>

=={{header|Picat}}==
A string is a list of characters. The string literal is in double quotes (a character is in single quote, e.g 's'):
<pre>"string"</pre>

It can also be constructed as a list of characters:
<pre>['s','t','r','i','n','g']</pre>

or as a list of (character) atoms (without single quotes):
<pre>[s,t,r,i,n,g]</pre>

However, upper case characters must be quoted (otherwise they are considered variables):
<pre>['S',t,r,i,n,g]</pre>


Quoting of certain characters are with an escape character (<code>\c</code>):

<pre>"a string\'s quotes: \"a string\'s quotes\". Spaces: \t\n\l\r"</pre>

A string can be written on several lines where the newlines are kept:
<syntaxhighlight lang="picat"> X = "string with
newlines and
spaces",
% ...
</syntaxhighlight>

Using a single <code>\</code> as the last character on a line makes the line continue without newline.
<syntaxhighlight lang="picat"> X = "string with \
newlines \
and \
spaces",
% ...
</syntaxhighlight>

is the same as
<pre>string with newlines and spaces</pre>



=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Line 1,908: Line 2,128:


Syntactically, transient symbols (called "strings" in the following) are surrounded by double quotes.
Syntactically, transient symbols (called "strings" in the following) are surrounded by double quotes.
<lang PicoLisp>: "ab\"cd"
<syntaxhighlight lang="picolisp">: "ab\"cd"
-> "ab\"cd"</lang>
-> "ab\"cd"</syntaxhighlight>
Double quotes in strings are escaped with a backslash.
Double quotes in strings are escaped with a backslash.


ASCII control characters can be written using the hat ('^') character:
ASCII control characters can be written using the hat ('^') character:
<lang PicoLisp>: "ab^Icd^Jef" # Tab, linefeed</lang>
<syntaxhighlight lang="picolisp">: "ab^Icd^Jef" # Tab, linefeed</syntaxhighlight>
There is no special character type or representation. Individual characters are handled as single-character strings:
There is no special character type or representation. Individual characters are handled as single-character strings:
<lang PicoLisp>: (chop "abc")
<syntaxhighlight lang="picolisp">: (chop "abc")
-> ("a" "b" "c")
-> ("a" "b" "c")


: (pack (reverse @))
: (pack (reverse @))
-> "cba"</lang>
-> "cba"</syntaxhighlight>
A limited handling of here-strings is available with the '[http://software-lab.de/doc/refH.html#here here]' function.
A limited handling of here-strings is available with the '[http://software-lab.de/doc/refH.html#here here]' function.


=={{header|Pike}}==
=={{header|Pike}}==
<lang pike>
<syntaxhighlight lang="pike">
'c'; // Character code (ASCII) (result: 99)
'c'; // Character code (ASCII) (result: 99)
"c"; // String (result: "c")
"c"; // String (result: "c")
Line 1,931: Line 2,151:
string using the
string using the
preprocessor" // single literal string with newlines in it
preprocessor" // single literal string with newlines in it
</syntaxhighlight>
</lang>


=={{header|PL/I}}==
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
'H' /* a single character as a literal. */
'H' /* a single character as a literal. */
'this is a string'
'this is a string'
Line 1,941: Line 2,161:
/* stored are <<John's cat>> */
/* stored are <<John's cat>> */
'101100'b /* a bit string, stored as one bit per digit. */
'101100'b /* a bit string, stored as one bit per digit. */
</syntaxhighlight>
</lang>

=={{header|Plain English}}==
A string literal is surrounded by double quotes. Plain English does not make a distinction between string and character literals.

To escape a double quote inside a string literal, use two double quotes.
<syntaxhighlight lang="text">"a ""string"" literal"</syntaxhighlight>


=={{header|plainTeX}}==
=={{header|plainTeX}}==


<syntaxhighlight lang="tex">`a' is for ``apple"
The same as [[Quotes#LaTeX|LaTeX case]], even though one should say the opposite.
\end</syntaxhighlight>

The same as [[Quotes#LaTeX|LaTeX case]], even though one should say the opposite.

The <tt>``</tt> and <tt><nowiki>''</nowiki></tt> in TeX (plainTeX, LaTeX and many more) are just examples of ligatures.
The <tt>``</tt> and <tt><nowiki>''</nowiki></tt> in TeX (plainTeX, LaTeX and many more) are just examples of ligatures.


Line 1,956: Line 2,186:
String are written in quotes
String are written in quotes


<lang pop11>'a' ;;; string consisting of single character</lang>
<syntaxhighlight lang="pop11">'a' ;;; string consisting of single character</syntaxhighlight>


Backslash is used to insert special charaters into strings:
Backslash is used to insert special charaters into strings:


<lang pop11>'\'\n' ;;; string consisting of quote and newline</lang>
<syntaxhighlight lang="pop11">'\'\n' ;;; string consisting of quote and newline</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
Line 1,975: Line 2,205:
Standard Prolog has no string types. It has atoms which can be formed in two ways, one of which is wrapping arbitrary text in single quotation marks:
Standard Prolog has no string types. It has atoms which can be formed in two ways, one of which is wrapping arbitrary text in single quotation marks:


<lang prolog>'This is an "atom" and not a string.'</lang>
<syntaxhighlight lang="prolog">'This is an "atom" and not a string.'</syntaxhighlight>


Such atoms can be (and are) treated as immutable strings in Prolog in many cases. Another string-like form wraps text in double quotation marks:
Such atoms can be (and are) treated as immutable strings in Prolog in many cases. Another string-like form wraps text in double quotation marks:


<lang prolog>"This 'string' will fool you if you're in a standard Prolog environment."</lang>
<syntaxhighlight lang="prolog">"This 'string' will fool you if you're in a standard Prolog environment."</syntaxhighlight>


While this appears as a string to non-Prolog users, it is in reality a linked list of integers with each node containing the integer value of the character (or for Unicode-capable systems, code point) at that location. For example:
While this appears as a string to non-Prolog users, it is in reality a linked list of integers with each node containing the integer value of the character (or for Unicode-capable systems, code point) at that location. For example:


<lang prolog>?- [97, 98, 99] = "abc".
<syntaxhighlight lang="prolog">?- [97, 98, 99] = "abc".
true.</lang>
true.</syntaxhighlight>


Individual character constants are special forms of integer (syntax sugar) using a 0' prefix:
Individual character constants are special forms of integer (syntax sugar) using a 0' prefix:


<lang prolog>?- 97 = 0'a.
<syntaxhighlight lang="prolog">?- 97 = 0'a.
true.</lang>
true.</syntaxhighlight>


{{works with|SWI Prolog|7.0}}
{{works with|SWI Prolog|7.0}}
Line 1,995: Line 2,225:
SWI-Prolog, beginning with version 7, introduced a new native string type. Unless options are specifically set by the user, character sequences wrapped in double quotes are now a string data type. The older list-based version uses back quotes instead:
SWI-Prolog, beginning with version 7, introduced a new native string type. Unless options are specifically set by the user, character sequences wrapped in double quotes are now a string data type. The older list-based version uses back quotes instead:


<lang prolog>?- [97, 98, 99] = "abc".
<syntaxhighlight lang="prolog">?- [97, 98, 99] = "abc".
false.
false.
?- [97, 98, 99] = `abc`.
?- [97, 98, 99] = `abc`.
true.</lang>
true.</syntaxhighlight>


Also starting with SWI-Prolog version 7, quasiquotation became possible. While not exactly a string type directly, they can be (ab)used to give multi-line strings. More importantly, however, they permit special string handling to be embedded into Prolog code, in effect permitting entire other languages inside of Prolog to be used natively as per this example:
Also starting with SWI-Prolog version 7, quasiquotation became possible. While not exactly a string type directly, they can be (ab)used to give multi-line strings. More importantly, however, they permit special string handling to be embedded into Prolog code, in effect permitting entire other languages inside of Prolog to be used natively as per this example:


<lang prolog>test_qq_odbc :-
<syntaxhighlight lang="prolog">test_qq_odbc :-
myodbc_connect_db(Conn),
myodbc_connect_db(Conn),
odbc_query(Conn, {|odbc||
odbc_query(Conn, {|odbc||
Line 2,014: Line 2,244:
C.category_id=G.category_id
C.category_id=G.category_id
|}, Row),
|}, Row),
writeln(Row).</lang>
writeln(Row).</syntaxhighlight>


In this example, the test_qq_odbc/0 predicate connects to an ODBC database and performs a query. The query is wrapped into a multi-line quasiquotation (beginning with {| and ending with |}) that checks the syntax and security of the query, so not only is the query a multi-line string, it is a **checked** multiline string in this case.
In this example, the test_qq_odbc/0 predicate connects to an ODBC database and performs a query. The query is wrapped into a multi-line quasiquotation (beginning with {| and ending with |}) that checks the syntax and security of the query, so not only is the query a multi-line string, it is a **checked** multiline string in this case.
Line 2,020: Line 2,250:
=={{header|PureBasic}}==
=={{header|PureBasic}}==
PureBasic supports char in ASCII and UNICODE as well as both dynamic and fixed length strings.
PureBasic supports char in ASCII and UNICODE as well as both dynamic and fixed length strings.
<lang PureBasic>; Characters (*.c), can be ASCII or UNICODE depending on compiler setting
<syntaxhighlight lang="purebasic">; Characters (*.c), can be ASCII or UNICODE depending on compiler setting
Define.c AChar='A'
Define.c AChar='A'
; defines as *.a it will be ASCII and *.u is always UNICODE
; defines as *.a it will be ASCII and *.u is always UNICODE
Line 2,035: Line 2,265:
; '"' can be included via CHR() or its predefined constant
; '"' can be included via CHR() or its predefined constant
Define AStringQuotes$=Chr(34)+"Buu"+Chr(34)+" said the ghost!"
Define AStringQuotes$=Chr(34)+"Buu"+Chr(34)+" said the ghost!"
Define BStringQuotes$=#DOUBLEQUOTE$+"Buu"+#DOUBLEQUOTE$+" said yet a ghost!"</lang>
Define BStringQuotes$=#DOUBLEQUOTE$+"Buu"+#DOUBLEQUOTE$+" said yet a ghost!"</syntaxhighlight>
To dynamically detect the current sizes of a character, e.g. ASCI or UNICODE mode, StringByteLength() can be used.
To dynamically detect the current sizes of a character, e.g. ASCI or UNICODE mode, StringByteLength() can be used.
<lang PureBasic>Select StringByteLength("X")
<syntaxhighlight lang="purebasic">Select StringByteLength("X")
Case 1
Case 1
Print("ASCII-mode; Soo, Hello world!")
Print("ASCII-mode; Soo, Hello world!")
Case 2
Case 2
Print("UNICODE-mode; Soo, 您好世界!")
Print("UNICODE-mode; Soo, 您好世界!")
EndSelect</lang>
EndSelect</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
Line 2,049: Line 2,279:
One can use single or double quotes.
One can use single or double quotes.


<lang python>'c' == "c" # character
<syntaxhighlight lang="python">'c' == "c" # character
'text' == "text"
'text' == "text"
' " '
' " '
Line 2,055: Line 2,285:
'\x20' == ' '
'\x20' == ' '
u'unicode string'
u'unicode string'
u'\u05d0' # unicode literal</lang>
u'\u05d0' # unicode literal</syntaxhighlight>


As shown in the last examples, Unicode strings
As shown in the last examples, Unicode strings
Line 2,063: Line 2,293:
This is useful when defining regular expressions as it avoids the need to use sequences like \\\\ (a sequence of four backslashes) in order to get one literal backslash into a regular expression string.
This is useful when defining regular expressions as it avoids the need to use sequences like \\\\ (a sequence of four backslashes) in order to get one literal backslash into a regular expression string.


<lang python>r'\x20' == '\\x20'</lang>
<syntaxhighlight lang="python">r'\x20' == '\\x20'</syntaxhighlight>


The Unicode and raw string modifiers can be combined to prefix a raw Unicode string. This '''must''' be done as "ur" or "UR" (not with the letters reversed as it: "ru").
The Unicode and raw string modifiers can be combined to prefix a raw Unicode string. This '''must''' be done as "ur" or "UR" (not with the letters reversed as it: "ru").
Line 2,069: Line 2,299:
Here-strings are denoted with triple quotes.
Here-strings are denoted with triple quotes.


<lang python>''' single triple quote '''
<syntaxhighlight lang="python">''' single triple quote '''
""" double triple quote """</lang>
""" double triple quote """</syntaxhighlight>


The "u" and "r" prefixes can also be used with triple quoted strings.
The "u" and "r" prefixes can also be used with triple quoted strings.
Line 2,077: Line 2,307:
They are terminated by unescaped triple quotes of the same type that initiated the expression.
They are terminated by unescaped triple quotes of the same type that initiated the expression.
They are generally used for "doc strings" and other multi-line string expressions --- and are useful for "commenting out" blocks of code.
They are generally used for "doc strings" and other multi-line string expressions --- and are useful for "commenting out" blocks of code.

=={{header|Quackery}}==

A character literal is denoted by the word <code>char</code>. The character is the first non-whitespace character following <code>char</code>.

A string literal is denoted by the word <code>$</code>. The string is delimited by the first non-whitespace character following <code>$</code>.

Character and string literals illustrated in the Quackery shell (REPL):
<pre>/O> char X emit
... char Y emit
... char Z emit cr
... $ "This is a 'string'." echo$ cr
... $ 'This is a "string" too.' echo$ cr
... $ ~This is one with "quotes" and 'apostrophes'.~ echo$ cr
... $ \Any non-whitespace character can be the delimiter.\ echo$ cr
...
XYZ
This is a 'string'.
This is a "string" too.
This is one with "quotes" and 'apostrophes'.
Any non-whitespace character can be the delimiter.
</pre>


=={{header|R}}==
=={{header|R}}==
Line 2,083: Line 2,335:
See [http://stat.ethz.ch/R-manual/R-patched/library/base/html/Quotes.html ?Quotes] for more information.
See [http://stat.ethz.ch/R-manual/R-patched/library/base/html/Quotes.html ?Quotes] for more information.


<lang R>str1 <- "the quick brown fox, etc."
<syntaxhighlight lang="r">str1 <- "the quick brown fox, etc."
str2 <- 'the quick brown fox, etc.'
str2 <- 'the quick brown fox, etc.'
identical(str1, str2) #returns TRUE</lang>
identical(str1, str2) #returns TRUE</syntaxhighlight>


R also supports testing string literals with '''==''', e.g.,
R also supports testing string literals with '''==''', e.g.,


<lang R>modestring <- 'row,col'
<syntaxhighlight lang="r">modestring <- 'row,col'
mode.vec <- unlist(strsplit(modestring, ','))
mode.vec <- unlist(strsplit(modestring, ','))
mode.vec[1] # "row"
mode.vec[1] # "row"
mode.vec[2] # "col"
mode.vec[2] # "col"
if (mode.vec[2] == 'col') { cat('Col!\n') } # Col! (with no quotes)
if (mode.vec[2] == 'col') { cat('Col!\n') } # Col! (with no quotes)
if (mode.vec[1] == "row") { cat('Row!\n') } # Row!</lang>
if (mode.vec[1] == "row") { cat('Row!\n') } # Row!</syntaxhighlight>


R also uses backticks, for creating non-standard variable names (amongst other things).
R also uses backticks, for creating non-standard variable names (amongst other things).


<lang R>`a b` <- 4
<syntaxhighlight lang="r">`a b` <- 4
`a b` # 4
`a b` # 4
a b # Error: unexpected symbol in "a b"</lang>
a b # Error: unexpected symbol in "a b"</syntaxhighlight>


R will print different styles of single and double quote using sQuote and dQuote
R will print different styles of single and double quote using sQuote and dQuote


<lang R>options(useFancyQuotes=FALSE)
<syntaxhighlight lang="r">options(useFancyQuotes=FALSE)
cat("plain quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</lang>
cat("plain quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</syntaxhighlight>


returns
returns
Line 2,111: Line 2,363:
plain quotes: "double" and 'single'
plain quotes: "double" and 'single'


<lang R>options(useFancyQuotes=TRUE)
<syntaxhighlight lang="r">options(useFancyQuotes=TRUE)
cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</lang>
cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</syntaxhighlight>


returns
returns
Line 2,118: Line 2,370:
fancy quotes: “double” and ‘single’
fancy quotes: “double” and ‘single’


<lang R>options(useFancyQuotes="TeX")
<syntaxhighlight lang="r">options(useFancyQuotes="TeX")
cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</lang>
cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</syntaxhighlight>


returns
returns
Line 2,130: Line 2,382:
sometime using a name for the character.
sometime using a name for the character.


<syntaxhighlight lang="racket">
<lang Racket>
#\a
#\a
#\space
#\space
#\return
#\return
</syntaxhighlight>
</lang>


Strings are double-quoted, and have most of the usual C-style escapes.
Strings are double-quoted, and have most of the usual C-style escapes.
Line 2,151: Line 2,403:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
Unlike most languages that hardwire their quoting mechanisms, the quote mechanism in Perl 6 is extensible, and all normal-looking quotes actually derive from a parent quoting language called Q via grammatical mixins, applied via standard Perl 6 adverbial syntax.
Unlike most languages that hardwire their quoting mechanisms, the quote mechanism in Raku is extensible, and all normal-looking quotes actually derive from a parent quoting language called Q via grammatical mixins, applied via standard Raku adverbial syntax.
The available quote mixins, straight from current spec S02, are:
The available quote mixins, straight from current spec S02, are:
<pre>Short Long Meaning
<pre>Short Long Meaning
Line 2,175: Line 2,427:
In any case, an initial <tt>Q</tt>, <tt>q</tt>, or <tt>qq</tt> may omit the initial colon to form traditional Perl quotes such as <tt>qw//</tt>.
In any case, an initial <tt>Q</tt>, <tt>q</tt>, or <tt>qq</tt> may omit the initial colon to form traditional Perl quotes such as <tt>qw//</tt>.
And Q can be used by itself to introduce a quote that has no escapes at all except for the closing delimiter:
And Q can be used by itself to introduce a quote that has no escapes at all except for the closing delimiter:
<lang perl6>my $raw = Q'$@\@#)&!#';</lang>
<syntaxhighlight lang="raku" line>my $raw = Q'$@\@#)&!#';</syntaxhighlight>
Note that the single quotes there imply no single quoting semantics as they would in Perl 5. They're just the quotes the programmer happened to choose, since they were most like the raw quoting. Single quotes imply <tt>:q</tt> only when used as normal single quotes are, as discussed below.
Note that the single quotes there imply no single quoting semantics as they would in Perl 5. They're just the quotes the programmer happened to choose, since they were most like the raw quoting. Single quotes imply <tt>:q</tt> only when used as normal single quotes are, as discussed below.
As in Perl 5, you can use any non-alphanumeric, non-whitespace characters for delimiters with the general forms of quoting, including matching bracket characters, including any Unicode brackets.
As in Perl 5, you can use any non-alphanumeric, non-whitespace characters for delimiters with the general forms of quoting, including matching bracket characters, including any Unicode brackets.
Line 2,192: Line 2,444:
The <tt>:qq</tt>-derived languages all give normal Perlish interpolation, but individual interpolations may be chosen or suppressed with extra adverbs.
The <tt>:qq</tt>-derived languages all give normal Perlish interpolation, but individual interpolations may be chosen or suppressed with extra adverbs.


Unlike in Perl 5, we don't use backticks as shorthand for what is now expressed as <tt>qqx//</tt> in Perl 6.
Unlike in Perl 5, we don't use backticks as shorthand for what is now expressed as <tt>qqx//</tt> in Raku.
(Backticks are now reserved for user-defined syntax.)
(Backticks are now reserved for user-defined syntax.)
Heredocs now have no special <tt><<</tt> syntax,
Heredocs now have no special <tt><<</tt> syntax,
but fall out of the <tt>:to</tt> adverb:
but fall out of the <tt>:to</tt> adverb:
<lang perl6>say qq:to/END/;
<syntaxhighlight lang="raku" line>say qq:to/END/;
Your ad here.
Your ad here.
END</lang>
END</syntaxhighlight>
Indentation equivalent to the ending tag is automatically removed.
Indentation equivalent to the ending tag is automatically removed.


Backslash sequences recognized by <tt>:b</tt> (and hence <tt>:qq</tt>) include:
Backslash sequences recognized by <tt>:b</tt> (and hence <tt>:qq</tt>) include:
<lang perl6>"\a" # BELL
<syntaxhighlight lang="raku" line>"\a" # BELL
"\b" # BACKSPACE
"\b" # BACKSPACE
"\t" # TAB
"\t" # TAB
Line 2,216: Line 2,468:
"\c8" # BACKSPACE
"\c8" # BACKSPACE
"\c[13,10]" # CRLF
"\c[13,10]" # CRLF
"\c[LATIN CAPITAL LETTER A, COMBINING RING ABOVE]"</lang>
"\c[LATIN CAPITAL LETTER A, COMBINING RING ABOVE]"</syntaxhighlight>
Leading <tt>0</tt> specifically does not mean octal in Perl 6;
Leading <tt>0</tt> specifically does not mean octal in Perl 6;
you must use <tt>\o</tt> instead.
you must use <tt>\o</tt> instead.
Line 2,225: Line 2,477:
ASCII characters are prefixed by a single dollar sign.
ASCII characters are prefixed by a single dollar sign.


<syntaxhighlight lang="retro">$c
<lang Retro>$c
'hello,_world!
'hello,_world!
'This_is_'a_string'
'This_is_'a_string'
</syntaxhighlight>
</lang>


=={{header|REXX}}==
=={{header|REXX}}==
Line 2,236: Line 2,488:
There is no difference between them as far as specifying a REXX literal.
There is no difference between them as far as specifying a REXX literal.
<br>You can double them (code two of them adjacent) to specify a quote within the string.
<br>You can double them (code two of them adjacent) to specify a quote within the string.
<lang rexx>char1 = "A"
<syntaxhighlight lang="rexx">char1 = "A"
char2 = 'A'
char2 = 'A'
str = "this is a string"
str = "this is a string"
another = 'this is also a string'
another = 'this is also a string'
escape1 = "that's it!"
escape1 = "that's it!"
escape2 = 'that''s it!'</lang>
escape2 = 'that''s it!'</syntaxhighlight>
Variable expansion is not possible within REXX literals.
Variable expansion is not possible within REXX literals.
<br>Simply concatenate the string with the variable:
<br>Simply concatenate the string with the variable:
<lang rexx>amount = 100
<syntaxhighlight lang="rexx">amount = 100
result = "You got" amount "points."
result = "You got" amount "points."
say result</lang>
say result</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,252: Line 2,504:
</pre>
</pre>
It's also possible to express characters in hexadecimal notation in a string:
It's also possible to express characters in hexadecimal notation in a string:
<lang rexx>lf = '0A'x
<syntaxhighlight lang="rexx">lf = '0A'x
cr = '0D'x
cr = '0D'x


Line 2,258: Line 2,510:
ppp = 'dead beaf 11112222 33334444 55556666 77778888 00009999 c0ffee'X
ppp = 'dead beaf 11112222 33334444 55556666 77778888 00009999 c0ffee'X


lang = '52455858'x /*which is "REXX" on ASCII-computers.*/</lang>
lang = '52455858'x /*which is "REXX" on ASCII-computers.*/</syntaxhighlight>
Binary strings are also possible:
Binary strings are also possible:
<lang rexx>jjj = '01011011'B
<syntaxhighlight lang="rexx">jjj = '01011011'B
jjj = '01011011'b
jjj = '01011011'b
jjj = "0101 1011"b
jjj = "0101 1011"b
jjj = '0101 1011 1111'b
jjj = '0101 1011 1111'b
longjjj = '11110000 10100001 10110010 11100011 11100100'B</lang>
longjjj = '11110000 10100001 10110010 11100011 11100100'B</syntaxhighlight>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
see 'This is a "quoted string"'
see 'This is a "quoted string"'
</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Quotes that do not interpolate:
Quotes that do not interpolate:
<lang ruby>'single quotes with \'embedded quote\' and \\backslash'
<syntaxhighlight lang="ruby">'single quotes with \'embedded quote\' and \\backslash'
%q(not interpolating with (nested) parentheses
%q(not interpolating with (nested) parentheses
and newline)</lang>
and newline)</syntaxhighlight>


Quotes that interpolate:
Quotes that interpolate:
<lang ruby>a = 42
<syntaxhighlight lang="ruby">a = 42
"double quotes with \"embedded quote\"\nnewline and variable interpolation: #{a} % 10 = #{a % 10}"
"double quotes with \"embedded quote\"\nnewline and variable interpolation: #{a} % 10 = #{a % 10}"
%Q(same as above)
%Q(same as above)
%|same as above|</lang>
%|same as above|</syntaxhighlight>


Heredocs
Heredocs
<lang ruby>print <<HERE
<syntaxhighlight lang="ruby">print <<HERE
With an unquoted delimiter, this interpolates:
With an unquoted delimiter, this interpolates:
a = #{a}
a = #{a}
Line 2,293: Line 2,545:
print <<'NON_INTERPOLATING'
print <<'NON_INTERPOLATING'
This will not interpolate: #{a}
This will not interpolate: #{a}
NON_INTERPOLATING</lang>
NON_INTERPOLATING</syntaxhighlight>

=={{header|Rust}}==

A <code>char</code> in Rust is a Unicode scalar value. A char type in Rust is always four bytes in size and can be denoted by single quotes:

<syntaxhighlight lang="rust">
let char01: char = 'a';
let char02: char = '\u{25A0}'; // Black square
let char03: char = '❤'; // Heart
</syntaxhighlight>

Rust has two common string types: <code>&str</code> and <code>String</code>. These different string types are used depending if it's a fixed string literal that is saved into the executable and lives throughout the program execution (usually borrowed as a <code>&str</code>), a string slice that references a contiguous sequence of elements (borrowed as a <code>&str</code>), or a <code>String</code> which allows for a growable heap allocated valid UTF-8 string. Only the <code>String</code> type is fully owned by its variable, and the other two are only interacted through the reference <code>&str</code>. The <code>&str</code> string slice type may point to a string literal or a heap-allocated string.

The String type in Rust is intended to always contain a valid UTF-8 string. UTF-8 is a "variable width" encoding, and therefore Strings are going to be typically smaller than an array of the same Rust <code>char</code>'s. For example, the String "hi" is all within ASCII, and therefore a UTF-8 String of "hi" represents each character as one byte each. On the other hand, a char array ['h', 'i'] would take up 8 bytes in total. A string is denoted by double quotes:

<syntaxhighlight lang="rust">
const string_literal_str1: &str = "Hi Rust!";

let string_slice_str1: &str = string_literal_str1; // Creating a string slice from a string literal
let string_slice_str2: &str = "hello str"; // String slice pointing to string literal "hello str"

let string1: String = String::new(); // Empty String
let string2: String = String::from("hello"); // Creating String from string literal "hello"
let string3: String = "hi".to_string();
let string4: String = "bye".to_owned();
let string5: String = "see you soon".into();
// The "to_string()", "to_owned" or "into" are all equivalent in the code above.
// The "to_string()", "to_owned" or "into" methods are needed so that a string slice (&str) or a string literal (&str) is explicitly converted into a heap-allocated fully-owned String type. Otherwise the compiler's type checker will complain "expected struct `String`, found `&str` (string slice)"

let string6: String = string_slice_str2.to_owned(); // Explictly converting the string_slice_str2 into a heap-allocated fully-owned String. This can be done with "to_string()", "to_owned" or "into".

// String slices can also point to heap allocated strings:
let string_slice_str3: &str = &string2; // Creating a string slice to a heap-allocated String.
let string7: String = string_slice_str3.to_string(); // Converting string_slice_str3 into a heap-allocated fully-owned String copy, resulting in a new independent owned string copy of the original String. This can be done with "to_string()", "to_owned" or "into".
</syntaxhighlight>

Rust supports verbatim strings and here-strings by putting <code>r#</code> before the first double quotes, and the end is marked by adding a <code>#</code> after the final double quotes. If there is a <code>"#</code> inside the contents of your here-string or your verbatim string then you can just add more <code>#</code>'s as required in both the beggining and the end:
<syntaxhighlight lang="rust">
let verbatim_here_string01: &str = r#"A \verbatim string\, line breaks in programming use \n and tabs \t"#;
let verbatim_here_string02: &str = r#"
A \multi-line\ string, in programming
line breaks use the characters \n
and for tabs we use the characters \t
"#;
let verbatim_here_string03: &str = r##"
Part number "#001": 1
Part number "#002": 2
Part number "#003": 3
"##;
</syntaxhighlight>

To expand variables in Rust we have 3 options: we can use the "format!" macro, the "print!" macro or the "println!" macro.
<syntaxhighlight lang="rust">
let number: i32 = 42;
let number_string: String = format!("Number: {}", number);
println!("The result in string form is '{}'.", number_string);
print!("The number is {}. ", number); // Print without line break
println!("Again, it's {}", number); // Print with line break
// The above prints:
// The result in string form is 'Number: 42'.
// The number is 42. Again, it's 42
</syntaxhighlight>

In Rust, there are other string types that are specialized to specific string requirements, such as working with system strings (OsString and OsStr), working with C strings (CString and CStr), and working with system paths (Path and PathBuf).


=={{header|S-lang}}==
=={{header|S-lang}}==
Line 2,308: Line 2,624:
* $ to request dollar prefix variable substitutions within the given string.
* $ to request dollar prefix variable substitutions within the given string.


<lang C>% String literals
<syntaxhighlight lang="c">% String literals
variable c, ch, s, b, r, v;
variable c, ch, s, b, r, v;


Line 2,354: Line 2,670:
() = fputs(b, stdout);
() = fputs(b, stdout);
() = fputs("\n", stdout);
() = fputs("\n", stdout);
printf("strlen(b) is %d, bstrlen(b) is %d\n", strlen(b), bstrlen(b));</lang>
printf("strlen(b) is %d, bstrlen(b) is %d\n", strlen(b), bstrlen(b));</syntaxhighlight>


{{out}}
{{out}}
Line 2,400: Line 2,716:
Character literals use single quotes marks:
Character literals use single quotes marks:


<lang scala>val c = 'c'</lang>
<syntaxhighlight lang="scala">val c = 'c'</syntaxhighlight>


However, symbols are denoted with a single quote,
However, symbols are denoted with a single quote,
so care must be taken not to confuse the two:
so care must be taken not to confuse the two:


<lang scala>val sym = 'symbol</lang>
<syntaxhighlight lang="scala">val sym = 'symbol</syntaxhighlight>


Strings can use either double quotes, or three successive double quotes.
Strings can use either double quotes, or three successive double quotes.
The first allows special characters, the second doesn't:
The first allows special characters, the second doesn't:


<lang scala>scala> "newline and slash: \n and \\"
<syntaxhighlight lang="scala">scala> "newline and slash: \n and \\"
res5: java.lang.String =
res5: java.lang.String =
newline and slash:
newline and slash:
Line 2,416: Line 2,732:


scala> """newline and slash: \n and \\"""
scala> """newline and slash: \n and \\"""
res6: java.lang.String = newline and slash: \n and \\</lang>
res6: java.lang.String = newline and slash: \n and \\</syntaxhighlight>


However, Unicode characters are expanded wherever they happen, even inside comments.
However, Unicode characters are expanded wherever they happen, even inside comments.
So, for instance:
So, for instance:


<lang scala>scala> val uniquote = \u0022normal string"
<syntaxhighlight lang="scala">scala> val uniquote = \u0022normal string"
uniquote: java.lang.String = normal string
uniquote: java.lang.String = normal string


scala> val insidequote = """an inside \u0022 quote"""
scala> val insidequote = """an inside \u0022 quote"""
insidequote: java.lang.String = an inside " quote</lang>
insidequote: java.lang.String = an inside " quote</syntaxhighlight>


Finally, on version 2.7, the triple-double-quoted string ends at the third consecutive quote, on version 2.8 it ends on the last quote of a series of at least three double-quotes.
Finally, on version 2.7, the triple-double-quoted string ends at the third consecutive quote, on version 2.8 it ends on the last quote of a series of at least three double-quotes.


'''Scala 2.7'''
'''Scala 2.7'''
<lang scala>scala> val error = """can't finish with a quote: """"
<syntaxhighlight lang="scala">scala> val error = """can't finish with a quote: """"
<console>:1: error: unterminated string
<console>:1: error: unterminated string
val error = """can't finish with a quote: """"
val error = """can't finish with a quote: """"
^</lang>
^</syntaxhighlight>


'''Scala 2.8'''
'''Scala 2.8'''
<lang scala>scala> val success = """but it can on 2.8: """"
<syntaxhighlight lang="scala">scala> val success = """but it can on 2.8: """"
success: java.lang.String = but it can on 2.8: "</lang>
success: java.lang.String = but it can on 2.8: "</syntaxhighlight>


=={{header|Scheme}}==
=={{header|Scheme}}==


Characters are specified using the "#\" syntax:
Characters are specified using the "#\" syntax:
<lang scheme>#\a
<syntaxhighlight lang="scheme">#\a
#\A
#\A
#\?
#\?
#\space
#\space
#\newline</lang>
#\newline</syntaxhighlight>


Strings are contained in double quotes:
Strings are contained in double quotes:
<lang scheme>"Hello world"</lang>
<syntaxhighlight lang="scheme">"Hello world"</syntaxhighlight>


Literal symbols, lists, pairs, etc. can be quoted using the quote syntax:
Literal symbols, lists, pairs, etc. can be quoted using the quote syntax:
<lang scheme>'apple
<syntaxhighlight lang="scheme">'apple
'(1 2 3) ; same as (list 1 2 3)
'(1 2 3) ; same as (list 1 2 3)
'() ; empty list
'() ; empty list
'(a . b) ; same as (cons 'a 'b)</lang>
'(a . b) ; same as (cons 'a 'b)</syntaxhighlight>


=={{header|Seed7}}==
=={{header|Seed7}}==
Line 2,462: Line 2,778:
A [http://seed7.sourceforge.net/manual/tokens.htm#Character_literals character literal] is written as UTF-8 encoded Unicode character enclosed in single quotes.
A [http://seed7.sourceforge.net/manual/tokens.htm#Character_literals character literal] is written as UTF-8 encoded Unicode character enclosed in single quotes.


<lang seed7>var char: ch is 'z';</lang>
<syntaxhighlight lang="seed7">var char: ch is 'z';</syntaxhighlight>


The type [http://seed7.sourceforge.net/manual/types.htm#string string] describes sequences of Unicode characters.
The type [http://seed7.sourceforge.net/manual/types.htm#string string] describes sequences of Unicode characters.
Line 2,468: Line 2,784:
A [http://seed7.sourceforge.net/manual/tokens.htm#String_literals string literal] is a sequence of UTF-8 encoded Unicode characters surrounded by double quotes.
A [http://seed7.sourceforge.net/manual/tokens.htm#String_literals string literal] is a sequence of UTF-8 encoded Unicode characters surrounded by double quotes.


<lang seed7>var string: stri is "hello";</lang>
<syntaxhighlight lang="seed7">var string: stri is "hello";</syntaxhighlight>


This means that 'z' and "z" are different.
This means that 'z' and "z" are different.
Line 2,509: Line 2,825:
Note that the integer literal is interpreted decimal unless it is written as [http://seed7.sourceforge.net/manual/types.htm#based_integer based integer].
Note that the integer literal is interpreted decimal unless it is written as [http://seed7.sourceforge.net/manual/types.htm#based_integer based integer].


<lang seed7>"Euro sign: \8364;"</lang>
<syntaxhighlight lang="seed7">"Euro sign: \8364;"</syntaxhighlight>


There is also a possibility to break a string into several lines.
There is also a possibility to break a string into several lines.


<lang seed7>var string: example is "this is a string\
<syntaxhighlight lang="seed7">var string: example is "this is a string\
\ which continues in the next line\n\
\ which continues in the next line\n\
\and contains a line break";</lang>
\and contains a line break";</syntaxhighlight>


There is no built-in mechanism for expanding variables within strings.
There is no built-in mechanism for expanding variables within strings.
Line 2,521: Line 2,837:
=={{header|Sidef}}==
=={{header|Sidef}}==
Quotes that do not interpolate:
Quotes that do not interpolate:
<lang ruby>'single quotes with \'embedded quote\' and \\backslash';
<syntaxhighlight lang="ruby">'single quotes with \'embedded quote\' and \\backslash';
‚unicode single quoted’;
‚unicode single quoted’;
%q(not interpolating with (nested) parentheses
%q(not interpolating with (nested) parentheses
and newline);</lang>
and newline);</syntaxhighlight>


Quotes that interpolate:
Quotes that interpolate:
<lang ruby>var a = 42;
<syntaxhighlight lang="ruby">var a = 42;
"double \Uquotes\E with \"embedded quote\"\nnewline and variable interpolation: #{a} % 10 = #{a % 10}";
"double \Uquotes\E with \"embedded quote\"\nnewline and variable interpolation: #{a} % 10 = #{a % 10}";
„same as above”;
„same as above”;
%Q(same as above);</lang>
%Q(same as above);</syntaxhighlight>


Heredocs:
Heredocs:
<lang ruby>print <<EOT
<syntaxhighlight lang="ruby">print <<EOT
Implicit double-quoted (interpolates):
Implicit double-quoted (interpolates):
a = #{a}
a = #{a}
Line 2,545: Line 2,861:
print <<'NON_INTERPOLATING'
print <<'NON_INTERPOLATING'
This will not interpolate: #{a}
This will not interpolate: #{a}
NON_INTERPOLATING</lang>
NON_INTERPOLATING</syntaxhighlight>


=={{header|Slate}}==
=={{header|Slate}}==
Line 2,551: Line 2,867:
Characters are specified using the <tt>$</tt> syntax:
Characters are specified using the <tt>$</tt> syntax:


<lang slate>$a
<syntaxhighlight lang="slate">$a
$D
$D
$8
$8
$,
$,
$\s
$\s
$\n</lang>
$\n</syntaxhighlight>


Strings are contained in single quotes, with backslash for escaping:
Strings are contained in single quotes, with backslash for escaping:
<lang slate>'Hello\'s the word.'</lang>
<syntaxhighlight lang="slate">'Hello\'s the word.'</syntaxhighlight>


=={{header|SQL}}==
=={{header|SQL}}==
String literals in SQL use single-quotation. There are no escapes, but you can double a <tt>'</tt> mark to make a single <tt>'</tt> in the text.
String literals in SQL use single-quotation. There are no escapes, but you can double a <tt>'</tt> mark to make a single <tt>'</tt> in the text.
<lang sql>SELECT 'The boy said ''hello''.';</lang>
<syntaxhighlight lang="sql">SELECT 'The boy said ''hello''.';</syntaxhighlight>


=={{header|Standard ML}}==
=={{header|Standard ML}}==


Characters are contained in the <code>#""</code> syntax:
Characters are contained in the <code>#""</code> syntax:
<lang sml>- #"a";
<syntaxhighlight lang="sml">- #"a";
val it = #"a" : char</lang>
val it = #"a" : char</syntaxhighlight>


Strings are contained in double quotes:
Strings are contained in double quotes:
<lang sml>- "Hello world";
<syntaxhighlight lang="sml">- "Hello world";
val it = "Hello world" : string</lang>
val it = "Hello world" : string</syntaxhighlight>


Strings may be split across lines and concatenated
Strings may be split across lines and concatenated
by having two backslashes around the newline and whitespace:
by having two backslashes around the newline and whitespace:
<lang sml>- "abc\
<syntaxhighlight lang="sml">- "abc\
\def";
\def";
val it = "abcdef" : string</lang>
val it = "abcdef" : string</syntaxhighlight>


=={{header|Swift}}==
=={{header|Swift}}==
<lang Swift>let you = "You"
<syntaxhighlight lang="swift">let you = "You"
let str1 = "\(you) can insert variables into strings."
let str1 = "\(you) can insert variables into strings."
let str2 = "Swift also supports unicode in strings ı∫ƒ∂ß´™¡à"
let str2 = "Swift also supports unicode in strings ı∫ƒ∂ß´™¡à"
Line 2,588: Line 2,904:
let str4 = "'" // '
let str4 = "'" // '
let str5 = "\"" // "
let str5 = "\"" // "
println(str3)</lang>
println(str3)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,598: Line 2,914:
Swift 4 introduced multi-line string literals called long strings. Long strings are strings delimited by <code>"""triple quotes"""</code> that can contain newlines and individual <code>"</code> characters without the need to escape them.
Swift 4 introduced multi-line string literals called long strings. Long strings are strings delimited by <code>"""triple quotes"""</code> that can contain newlines and individual <code>"</code> characters without the need to escape them.


<lang Swift>let author = "Author"
<syntaxhighlight lang="swift">let author = "Author"
let xml == """
let xml == """
<?xml version="1.0"?>
<?xml version="1.0"?>
Line 2,613: Line 2,929:
"""
"""


println(xml)</lang>
println(xml)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,636: Line 2,952:


Double quotes allow command and variable interpolation:
Double quotes allow command and variable interpolation:
<lang tcl>set str "This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]"
<syntaxhighlight lang="tcl">set str "This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]"
puts $str ;# ==> This is Tcl 8.5 It is Mon Apr 06 16:49:46 EDT 2009</lang>
puts $str ;# ==> This is Tcl 8.5 It is Mon Apr 06 16:49:46 EDT 2009</syntaxhighlight>


Braces prevent interpolation
Braces prevent interpolation
<lang tcl>set str {This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]}
<syntaxhighlight lang="tcl">set str {This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]}
puts $str ;# ==> This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]</lang>
puts $str ;# ==> This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]</syntaxhighlight>


=={{header|TI-89 BASIC}}==
=={{header|TI-89 BASIC}}==
Line 2,652: Line 2,968:


'''Basic strings''' are surrounded by quotation marks. Any Unicode character may be used except those that must be escaped: quotation mark, backslash, and the control characters other than tab (U+0000 to U+0008, U+000A to U+001F, U+007F).
'''Basic strings''' are surrounded by quotation marks. Any Unicode character may be used except those that must be escaped: quotation mark, backslash, and the control characters other than tab (U+0000 to U+0008, U+000A to U+001F, U+007F).
<lang TOML>str = "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."</lang>
<syntaxhighlight lang="toml">str = "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."</syntaxhighlight>


'''Multi-line basic strings''' are surrounded by three quotation marks on each side and allow newlines. A newline immediately following the opening delimiter will be trimmed. All other whitespace and newline characters remain intact.
'''Multi-line basic strings''' are surrounded by three quotation marks on each side and allow newlines. A newline immediately following the opening delimiter will be trimmed. All other whitespace and newline characters remain intact.
<lang TOML>str1 = """
<syntaxhighlight lang="toml">str1 = """
Roses are red
Roses are red
Violets are blue"""</lang>
Violets are blue"""</syntaxhighlight>


When the last non-whitespace character on a line is a <code>\</code> ("line ending backslash"), it will be trimmed along with all whitespace (including newlines) up to the next non-whitespace character or closing delimiter. All of the escape sequences that are valid for basic strings are also valid for multi-line basic strings.
When the last non-whitespace character on a line is a <code>\</code> ("line ending backslash"), it will be trimmed along with all whitespace (including newlines) up to the next non-whitespace character or closing delimiter. All of the escape sequences that are valid for basic strings are also valid for multi-line basic strings.
<lang TOML># The following strings are byte-for-byte equivalent:
<syntaxhighlight lang="toml"># The following strings are byte-for-byte equivalent:
str1 = "The quick brown fox jumps over the lazy dog."
str1 = "The quick brown fox jumps over the lazy dog."


Line 2,674: Line 2,990:
fox jumps over \
fox jumps over \
the lazy dog.\
the lazy dog.\
"""</lang>
"""</syntaxhighlight>


'''Literal strings''' are surrounded by single quotes and do not support escaping. This means that there is no way to write a single quote in a literal string. Like basic strings, they must appear on a single line.
'''Literal strings''' are surrounded by single quotes and do not support escaping. This means that there is no way to write a single quote in a literal string. Like basic strings, they must appear on a single line.
<lang TOML># What you see is what you get.
<syntaxhighlight lang="toml"># What you see is what you get.
winpath = 'C:\Users\nodejs\templates'
winpath = 'C:\Users\nodejs\templates'
winpath2 = '\\ServerX\admin$\system32\'
winpath2 = '\\ServerX\admin$\system32\'
quoted = 'Tom "Dubs" Preston-Werner'
quoted = 'Tom "Dubs" Preston-Werner'
regex = '<\i\c*\s*>'</lang>
regex = '<\i\c*\s*>'</syntaxhighlight>


'''Multi-line literal strings''' are surrounded by three single quotes on each side and allow newlines. Like literal strings, there is no escaping whatsoever. A newline immediately following the opening delimiter will be trimmed. All other content between the delimiters is interpreted as-is without modification. One or two single quotes are allowed anywhere within a multi-line literal string, but sequences of three or more single quotes are not permitted.
'''Multi-line literal strings''' are surrounded by three single quotes on each side and allow newlines. Like literal strings, there is no escaping whatsoever. A newline immediately following the opening delimiter will be trimmed. All other content between the delimiters is interpreted as-is without modification. One or two single quotes are allowed anywhere within a multi-line literal string, but sequences of three or more single quotes are not permitted.
<lang TOML>regex2 = '''I [dw]on't need \d{2} apples'''
<syntaxhighlight lang="toml">regex2 = '''I [dw]on't need \d{2} apples'''
lines = '''
lines = '''
The first newline is
The first newline is
Line 2,690: Line 3,006:
All other whitespace
All other whitespace
is preserved.
is preserved.
'''</lang>
'''</syntaxhighlight>


=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>
<syntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT,{}
$$ MODE TUSCRIPT,{}
s1=*
s1=*
Line 2,706: Line 3,022:
show=JOIN(show)
show=JOIN(show)
PRINT show
PRINT show
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,727: Line 3,043:
enclosed with doublequotes and to put doublequote characters
enclosed with doublequotes and to put doublequote characters
in a string enclosed within singlequotes:
in a string enclosed within singlequotes:
<lang bash>echo "The boy said 'hello'."
<syntaxhighlight lang="bash">echo "The boy said 'hello'."
echo 'The girl said "hello" too.'</lang>
echo 'The girl said "hello" too.'</syntaxhighlight>


We can also use an escapesequence to put doublequote characters in an interpolated string:
We can also use an escapesequence to put doublequote characters in an interpolated string:


<lang bash>print "The man said \"hello\".";</lang>
<syntaxhighlight lang="bash">print "The man said \"hello\".";</syntaxhighlight>


=== Here documents ===
=== Here documents ===
Line 2,739: Line 3,055:
Here documents cannot be used to represent literal strings as an expression for variable assignment.
Here documents cannot be used to represent literal strings as an expression for variable assignment.


<lang bash>cat << END
<syntaxhighlight lang="bash">cat << END
1, High Street,
1, High Street,
SMALLTOWN,
SMALLTOWN,
West Midlands.
West Midlands.
WM4 5HD.
WM4 5HD.
END</lang>
END</syntaxhighlight>


=={{header|Ursala}}==
=={{header|Ursala}}==


Single characters are denoted with a back quote.
Single characters are denoted with a back quote.
<lang Ursala>a = `x</lang>
<syntaxhighlight lang="ursala">a = `x</syntaxhighlight>
Unprintable character constants can be expressed like this.
Unprintable character constants can be expressed like this.
<lang Ursala>cr = 13%cOi&</lang>
<syntaxhighlight lang="ursala">cr = 13%cOi&</syntaxhighlight>
Strings are enclosed in single forward quotes.
Strings are enclosed in single forward quotes.
<lang Ursala>b = 'a string'</lang>
<syntaxhighlight lang="ursala">b = 'a string'</syntaxhighlight>
A single quote in a string is escaped by another single quote.
A single quote in a string is escaped by another single quote.
<lang Ursala>c = 'Hobson''s choice'</lang>
<syntaxhighlight lang="ursala">c = 'Hobson''s choice'</syntaxhighlight>
Multi-line strings are enclosed in dash-brackets.
Multi-line strings are enclosed in dash-brackets.
<syntaxhighlight lang="ursala">d =
<lang Ursala>d =


-[this is a list
-[this is a list
of strings]-</lang>
of strings]-</syntaxhighlight>
Dash-bracket enclosed text can have arbitrary nested
Dash-bracket enclosed text can have arbitrary nested
unquoted expressions, provided they evaluate to lists
unquoted expressions, provided they evaluate to lists
of character strings.
of character strings.
<lang Ursala>e = -[the front matter -[ d ]- the rest of it]-
<syntaxhighlight lang="ursala">e = -[the front matter -[ d ]- the rest of it]-


f = -[text -[ d ]- more -[ e ]- text ]-</lang>
f = -[text -[ d ]- more -[ e ]- text ]-</syntaxhighlight>
This notation can also be used for defining functions.
This notation can also be used for defining functions.
<lang Ursala>g "x" = -[ Dear -[ "x" ]- bla bla ]-</lang>
<syntaxhighlight lang="ursala">g "x" = -[ Dear -[ "x" ]- bla bla ]-</syntaxhighlight>
The double quotes aren't for character strings but
The double quotes aren't for character strings but
dummy variables.
dummy variables.
Line 2,776: Line 3,092:
A simple quoted string is of the form 'string'
A simple quoted string is of the form 'string'
e.g
e.g
<lang v>'hello world' puts</lang>
<syntaxhighlight lang="v">'hello world' puts</syntaxhighlight>


=={{header|Vim Script}}==
=={{header|Vim Script}}==
Line 2,791: Line 3,107:
{{works with|VBA|6.5}}
{{works with|VBA|6.5}}
{{works with|VBA|7.1}}
{{works with|VBA|7.1}}
<lang vb> Debug.Print "Tom said, ""The fox ran away."""
<syntaxhighlight lang="vb"> Debug.Print "Tom said, ""The fox ran away."""
Debug.Print "Tom said, 'The fox ran away.'"</lang>
Debug.Print "Tom said, 'The fox ran away.'"</syntaxhighlight>
{{out}}
{{out}}
<pre>Tom said, "The fox ran away."
<pre>Tom said, "The fox ran away."
Line 2,801: Line 3,117:
Visual Basic only supports single-line strings. The only escape sequence supported is the double double-quote (""), which is translated into a single double-quote.
Visual Basic only supports single-line strings. The only escape sequence supported is the double double-quote (""), which is translated into a single double-quote.


<lang vbnet>Dim s = "Tom said, ""The fox ran away."""
<syntaxhighlight lang="vbnet">Dim s = "Tom said, ""The fox ran away."""
Result: Tom said, "The fox ran away."</lang>
Result: Tom said, "The fox ran away."</syntaxhighlight>

=={{header|V (Vlang)}}==

Character literals for Unicode characters, "rune literals", are an alias for u32.

Character literals for UTF-8 characters, "string literals", are an alias for u8.

To denote rune, Unicode characters, ` (backticks) are used :

A rune can be converted to a UTF-8 string by using the .str() method.

rocket := `🚀`

rocket.str() == '🚀' // uses single quotes, not the backtick, after conversion

A string can be converted back to runes by the .runes() method.

hello := 'Hello World'

hello_runes := hello.runes() // [`H`, `e`, `l`, `l`, `o`, ` `, `W`, `o`, `r`, `l`, `d`]

In V, a string is an immutable array of read-only bytes. All Unicode characters are encoded using UTF-8:

mut s := 'hello 🌎'

s[0] = `H` // not allowed as immutable

// convert `string` to `[]u8`

s := 'hello 🌎'

arr := s.bytes()

assert arr.len == 10

// convert `[]u8` to `string`

s2 := arr.bytestr()

assert s2 == s

// indexing gives a byte, u8(66) == `B`

name := 'Bob'

println(name.len == 3) // will print 3

if name[0] == u8(66) {println(name[0].ascii_str())} // will print`B`

String literals are contained in quotes:

str:= "Hello, world!"


=={{header|WEB}}==
=={{header|WEB}}==
Line 2,817: Line 3,185:
All strings are instances of the built-in String class and there is no separate Character class as such. Characters are simply strings consisting of a single byte or Unicode code point (1 to 4 bytes).
All strings are instances of the built-in String class and there is no separate Character class as such. Characters are simply strings consisting of a single byte or Unicode code point (1 to 4 bytes).


String literals must be surrounded in double quotes and support the following escape characters:
''Ordinary'' string literals must be surrounded in double quotes and support the following escape characters:
{| class="wikitable"
{| class="wikitable"
! Character !! Meaning
! Character !! Meaning
Line 2,832: Line 3,200:
|-
|-
| \b || Backspace
| \b || Backspace
|-
| \e || ESC character
|-
|-
| \f || Form feed
| \f || Form feed
Line 2,850: Line 3,220:
|}
|}


String literals also allow interpolation. If you have a percent sign (%) followed by a parenthesized expression, the expression is evaluated and can be arbitrarily complex. Consequently, if you need to include a normal % character in a string literal, you have to use the escaped form \%.
''Ordinary'' string literals also allow interpolation. If you have a percent sign (%) followed by a parenthesized expression, the expression is evaluated and can be arbitrarily complex. Consequently, if you need to include a normal % character in a string literal, you have to use the escaped form \%.


From v0.4.0 Wren also supports ''raw'' string literals. These are any text surrounded by triple double quotes, """, and are interpreted verbatim i.e. any control codes and/or interpolations are not processed as such. They can include single or double double quotes without problem.
Wren doesn't support verbatim strings or here documents.
<lang ecmascript>var s = "abc123"
<syntaxhighlight lang="wren">var s = "abc123"
var t = "abc\t123\%"
var t = "abc\t123\%"
var u = "\U0001F64A\U0001F680"
var u = "\U0001F64A\U0001F680"
var v = "%("abc" * 3)"
var v = "%("abc" * 3)"
var w = """a"bc""def\n%(v)"""
System.print([s, t, u, v])</lang>

System.print([s, t, u, v, w])</syntaxhighlight>


{{out}}
{{out}}
<pre>
<pre>
[abc123, abc 123%, 🙊🚀, abcabcabc]
[abc123, abc 123%, 🙊🚀, abcabcabc, a"bc""def\n%(v)]
</pre>
</pre>


Line 2,868: Line 3,240:
Xojo only supports single-line strings. The only escape sequence supported is the double double-quote (""), which is translated into a single double-quote.
Xojo only supports single-line strings. The only escape sequence supported is the double double-quote (""), which is translated into a single double-quote.


<lang vbnet>Dim s As String = "Tom said, ""The fox ran away."""
<syntaxhighlight lang="vbnet">Dim s As String = "Tom said, ""The fox ran away."""
Result: Tom said, "The fox ran away."</lang>
Result: Tom said, "The fox ran away."</syntaxhighlight>


=={{header|XPL0}}==
=={{header|XPL0}}==
Line 2,923: Line 3,295:
=={{header|XSLT}}==
=={{header|XSLT}}==
XSLT is based on XML, and so can use either " or ' to delimit strings. Since XML attribute values are defined using double-quotes, one must use single-quotes for string literals within attributes.
XSLT is based on XML, and so can use either " or ' to delimit strings. Since XML attribute values are defined using double-quotes, one must use single-quotes for string literals within attributes.
<lang xml><xsl:if test="starts-with(@name, 'Mr.')">Mister</xsl:if></lang>
<syntaxhighlight lang="xml"><xsl:if test="starts-with(@name, 'Mr.')">Mister</xsl:if></syntaxhighlight>


Double and single quote characters may also be escaped with XML entities: &amp;quot; and &amp;apos; respectively.
Double and single quote characters may also be escaped with XML entities: &amp;quot; and &amp;apos; respectively.

=={{header|Z80 Assembly}}==
{{trans|6502 Assembly}}
Strings are enclosed in double quotes.
<syntaxhighlight lang="z80">db "Hello World"</syntaxhighlight>
Any typed character in double quotes is assembled as the ASCII equivalent of that character. Therefore the following two data blocks are equivalent:
<syntaxhighlight lang="z80">db "Hello World"
db $48,$65,$6c,$6c,$6f,$20,$57,$6f,$72,$6c,$64</syntaxhighlight>

The assembler typically assumes nothing with regard to special characters. A <code>\n</code> will be interpreted literally, for example. How special characters are handled depends on the printing routine of the hardware's BIOS, or one created by the programmer. If your printing routine is able to support a null terminator and ASCII control codes, the following represents "Hello World" with the new line command and null terminator:
<syntaxhighlight lang="z80">db "Hello World",13,10,0</syntaxhighlight>

Creating your own printing routine is a bit out of the scope of this task but here's a simple demonstration that supports the \n and null termination:
<syntaxhighlight lang="z80">PrintString:
; HL contains the pointer to the string literal.
ld a,(hl)
or a ;compares to zero quicker than "CP 0"
ret z ;we're done printing the string, so exit.
cp '\' ; a single ascii character is specified in single quotes. This compares A to the backslash's ASCII value.
jr z,HandleSpecialChars ; if accumulator = '\' then goto "HandleSpecialChars"
call PrintChar ;unimplemented print routine, depending on the system this is either a BIOS call
; or a routine written by the programmer.

inc hl ;next character
jr PrintString ;back to top


HandleSpecialChars:
inc hl ;next char
ld a,(hl)
cp 'n'
call z,NewLine ;unimplemented routine, advances text cursor to next line. Only called if accumulator = 'n'.
inc hl ;advance past the 'n' to the next char.
jr PrintString ;jump back to top. Notice that neither the backslash nor the character after it were actually printed.</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
Line 2,933: Line 3,339:


here-strings:
here-strings:
<lang zkl>text:=
<syntaxhighlight lang="zkl">text:=
0'|foo|
0'|foo|
"bar\n";</lang>
"bar\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
"foobar\n"
"foobar\n"
</pre>
</pre>
<lang zkl>n:=7; text:=String(
<syntaxhighlight lang="zkl">n:=7; text:=String(
"foo = ",3,"\n"
"foo = ",3,"\n"
"bar=",n,"\n"
"bar=",n,"\n"
);</lang>
);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>

Latest revision as of 10:52, 17 December 2023

Task
Literals/String
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Show literal specification of characters and strings.

If supported, show how the following work:

  •   verbatim strings   (quotes where escape sequences are quoted literally)
  •   here-strings  


Also, discuss which quotes expand variables.


Related tasks


Other tasks related to string operations:
Metrics
Counting
Remove/replace
Anagrams/Derangements/shuffling
Find/Search/Determine
Formatting
Song lyrics/poems/Mad Libs/phrases
Tokenize
Sequences



11l

Character literals:

V c = Char(‘a’)

Regular string literals are enclosed in double quotes, and use \ to delimit special characters:

"foo\nbar"

Raw string literals are enclosed in paired single quotation marks:

‘foo
bar’

If raw string literal should contains unpaired single quotation marks, then balancing of raw string should be performed:

'‘‘don’t’ // the same as "don’t"

6502 Assembly

Strings are enclosed in double quotes.

db "Hello World"

Any typed character in double quotes is assembled as the ASCII equivalent of that character. Therefore the following two data blocks are equivalent:

db "Hello World"
db $48,$65,$6c,$6c,$6f,$20,$57,$6f,$72,$6c,$64

When using a single-character literal as an operand for an instruction, it MUST have a # in front, or else the CPU will treat it as a pointer being dereferenced rather than a numeric constant. (We've all made this mistake at least once without realizing it.)

LDA #'A' ;load ascii code of "A" into the accumulator.
LDA 'A'  ;load the byte stored at memory address 0x41 into the accumulator.

The assembler typically assumes nothing with regard to special characters. A \n will be interpreted literally, for example. How special characters are handled depends on the printing routine of the hardware's BIOS, or one created by the programmer. If your printing routine is able to support a null terminator and ASCII control codes, the following represents "Hello World" with the new line command and null terminator:

db "Hello World",13,10,0

Creating your own printing routine is a bit out of the scope of this task but here's a simple demonstration that supports the \n and null termination:

PrintString:
lda (StringPtr),y
beq Terminated
cmp #'\'                  ; a single ascii character is specified in single quotes.
beq HandleSpecialChars
jsr PrintChar            ;unimplemented print routine
iny                      ;next character
jmp PrintString          ;back to top

Terminated: 
rts                      ;exit

HandleSpecialChars:
iny                      ;next char
lda (StringPtr),y
cmp #'n'
beq NextLine             ;unimplemented new line routine, it ends in "JMP DoneSpecialChar." 
                         ;Typically this would reset the x cursor and increment the y cursor, which are software variables that
                         ;get converted to a VRAM address in some other routine.
  
DoneSpecialChar:
iny
jmp PrintString         ;jump back to top. Notice that neither the backslash nor the character after it were actually printed.

68000 Assembly

Translation of: 6502 Assembly

Strings are enclosed in double quotes. C places a null terminator at the end of the string for you; in 68000 Assembly you have to type it manually (unless your assembler has an .ASCIZ directive or equivalent, and not all do).

DC.B "Hello World",0
EVEN

Any typed character in double quotes is assembled as the ASCII equivalent of that character. Therefore the following two data blocks are equivalent:

DC.B "Hello World",0
EVEN

DC.B $48,$65,$6c,$6c,$6f,$20,$57,$6f,$72,$6c,$64,$00
EVEN

When using a string literal as an operand for an instruction, it must begin with #, otherwise it will be treated as a pointer being dereferenced rather than the numeric constant you intended.

MOVE.L #'SEGA',D0  ;load the string "SEGA" into D0
MOVE.L '0000',D0   ;load the 32-bit value at address 0x00303030 (the most significant byte is always treated as zero,
                   ;because the 68000 only has a 24-bit address space.

The assembler typically assumes nothing with regard to special characters. How special characters are handled depends on the printing routine of the hardware's BIOS, or in the case of embedded hardware with no BIOS or a very limited one like the Sega Genesis, the printing routine created by the programmer. By default, there is no support for any control codes unless you add it in yourself.

Ada

Single character literals require single quotes

ch : character := 'a';

String literals use double quotes

msg : string := "hello world";
empty : string := "";  -- an empty string

The length of a string in Ada is equal to the number of characters in the string. Ada does not employ a terminating null character like C. A string can have zero length, but zero length strings are not often used. Ada's string type is a fixed length string. It cannot be extended after it is created. If you need to extend the length of a string you need to use either a bounded string, which has a pre-determined maximum length, similar to C strings, or an unbounded string which can expand or shrink to match the data it contains.

Aime

Aime has no character representation, but it allows single quoted character constants. Their implied typed is integer.

integer c;
c = 'z';

String literals are double quoted.

text s;
s = "z";

ALGOL 68

In ALGOL 68 a single character (CHAR), character arrays ([]CHAR) and strings (STRING) are contained in double quotes. ALGOL 68 also has FORMAT strings which are contained between dollar ($) symbols.

CHAR charx = "z";

Strings are contained in double quotes.

[]CHAR charxyz = "xyz";
STRING stringxyz = "xyz";
FORMAT twonewlines = $ll$, threenewpages=$ppp$, fourbackspaces=$bbbb$;

Note: When only uppercase characters sets are available (eg on computers with only 6 bits per "byte") the single quote can used to denote a reserved word. eg

.PR QUOTE .PR
[]'CHAR' CHARXYZ = "XYZ";

The STRING type is simply a FLEX array of CHAR.

MODE STRING = FLEX[1:0]CHAR;

ALGOL 68 also has raw strings called BYTES, this type is a fixed width packed array of CHAR.

BYTES bytesabc = bytes pack("abc");

A string quote character is inserted in a string when two quotes are entered, eg:

STRING stringquote = """I'll be back."" - The Terminator";

A string can span lines, but cannot contain newlines. String literals are concatenated when compiled:

STRING linexyz := "line X;" +
 "line Y;" + 
 "line Z;";

ALGOL 68 uses FORMATs for doing more advanced manipulations. For example given:

FILE linef; STRING line;
associate(linef, line);

Instead of using preprocessor macros ALGOL 68 can do FORMAT variable replacement within FORMATs at run time.

FORMAT my_symbol = $"SYMBOL"$;
FORMAT foo  = $"prefix_"f(my_symbol)"_suffix"$;
putf(linef ,foo);

In standard ALGOL 68 a "book" is a file. A book is composed of pages and lines and therefore a FORMAT be used for inserting backspaces, space, newlines and newpages into books.

INT pages=100, lines=25, characters=80;
FILE bookf; FLEX[pages]FLEX[lines]FLEX[characters]CHAR book;
associate(bookf, book);

# following putf inserts the string "    Line 4 indented 5" on page 3 #  
putf(bookf, $3p"Page 3"4l5x"Line 4 indented 5"$)

Note: ALGOL 68G does not implement newpage and backspace.

ALGOL W

begin
    % String literals are enclosed in double-quotes in Algol W.              %
    % There isn't a separate character type but strings of lenghth one can   %
    % be used instead.                                                       %
    % There are no escaping conventions used in string literals, except that %
    % in order to have a double-quote character in a string, two double      %
    % quotes must be used.                                                   %
    % Examples:                                                              %

    % write a single character                                               %
    write( "a" );

    % write a double-quote character                                         %
    write( """" );

    % write a multi-character string - note the "\" is not an escape         %
    % and a\nb will appear on the output, not a and b on separate lines      %
    write( "a\nb" );

end.
Output:
a
"
a\nb

ARM Assembly

Works with: as version Raspberry Pi
/* ARM assembly Raspberry PI  */
/*  program stringsEx.s   */

/* Constantes    */
.equ STDOUT, 1                           @ Linux output console
.equ EXIT,   1                           @ Linux syscall
.equ WRITE,  4                           @ Linux syscall

/* Initialized data */
.data
szMessString:            .asciz "String with final zero \n"
szMessString1:           .string "Other string with final zero \n"
sString:                 .ascii "String without final zero"
                         .byte 0             @ add final zero for display
sLineSpaces:             .byte '>'
                         .fill 10,1,' '      @ 10 spaces
                         .asciz "<\n"        @ add <, CR and final zero for display
sSpaces1:                .space 10,' '       @ other 10 spaces
                         .byte 0             @ add final zero for display
sCharA:                  .space 10,'A'       @ curious !! 10 A with space instruction
                         .asciz "\n"         @ add CR and final zero for display

cChar1:                  .byte 'A'           @ character A
cChar2:                  .byte 0x41          @ character A

szCarriageReturn:        .asciz "\n"

/* UnInitialized data */
.bss 

/*  code section */
.text
.global main 
main: 

    ldr r0,iAdrszMessString
    bl affichageMess                            @ display message
    ldr r0,iAdrszMessString1
    bl affichageMess
    ldr r0,iAdrsString
    bl affichageMess
    ldr r0,iAdrszCarriageReturn
    bl affichageMess
    ldr r0,iAdrsLineSpaces
    bl affichageMess
    ldr r0,iAdrsCharA
    bl affichageMess

100:                                            @ standard end of the program
    mov r0, #0                                  @ return code
    mov r7, #EXIT                               @ request to exit program
    svc 0                                       @ perform system call
iAdrszMessString:         .int szMessString
iAdrszMessString1:        .int szMessString1
iAdrsString:              .int sString
iAdrsLineSpaces:          .int sLineSpaces
iAdrszCarriageReturn:     .int szCarriageReturn
iAdrsCharA:               .int sCharA

/******************************************************************/
/*     display text with size calculation                         */ 
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
    push {r0,r1,r2,r7,lr}                       @ save  registers 
    mov r2,#0                                   @ counter length */
1:                                              @ loop length calculation
    ldrb r1,[r0,r2]                             @ read octet start position + index 
    cmp r1,#0                                   @ if 0 its over
    addne r2,r2,#1                              @ else add 1 in the length
    bne 1b                                      @ and loop 
                                                @ so here r2 contains the length of the message 
    mov r1,r0                                   @ address message in r1 
    mov r0,#STDOUT                              @ code to write to the standard output Linux
    mov r7, #WRITE                              @ code call system "write" 
    svc #0                                      @ call system
    pop {r0,r1,r2,r7,lr}                        @ restaur registers
    bx lr                                       @ return

Arturo

str: "Hello world"

print [str "->" type str]

fullLineStr: « This is a full-line string

print [fullLineStr "->" type fullLineStr]

multiline: {
    This
    is a multi-line
    string
}

print [multiline "->" type multiline]

verbatim: {:
    This is 
    a verbatim
    multi-line
    string
:}

print [verbatim "->" type verbatim]
Output:
Hello world -> :string 
This is a full-line string -> :string 
This
is a multi-line
string -> :string 
This is 
yet another
multi-line
string -> :string

AutoHotkey

unicode

"c" ; character
"text" ; string
hereString =   ; with interpolation of %variables%
(
"<>"
the time is %A_Now% 
\!
)

hereString2 =  ; with same line comments allowed, without interpolation of variables
(Comments %
literal %A_Now%  ; no interpolation here
)

AWK

In awk, strings are enclosed using doublequotes. Characters are just strings of length 1.

        c  = "x"
        str= "hello"
	s1 = "abcd"	# simple string
	s2 = "ab\"cd"	# string containing a double quote, escaped with backslash
	print s1
	print s2
Output:

Concatenation

$ awk 'BEGIN{c="x"; s="hello";s1 = "abcd"; s2 = "ab\"cd"; s=s c; print s; print s1; print s2}'
hellox

Axe

Character literal:

'A'

String literal:

"ABC"

Note that string literals are only null-terminated if they are assigned to a variable (e.g. Str1).

BASIC

Traditional BASIC implementations do not use literal character notation within a string or here document notation. However, literal characters can be referenced using their character code and these can be added to strings as required. Here we use the ASCII code for doublequotes to get the characters into a string:

10 LET Q$=CHR$(34): REM DOUBLEQUOTES
20 LET D$=Q$+Q$: REM A PAIR OF DOUBLEQUOTES
30 LET S$=Q$+"THIS IS A QUOTED STRING"+Q$
40 PRINT Q$;"HELLO";Q$:REM ADD QUOTES DURING OUTPUT

Most modern BASIC implementations don't differentiate between characters and strings -- a character is just a string of length 1. Few (if any) BASIC implementations support variables inside strings; instead, they must be handled outside the quotes. Most BASICs don't support escaping inside the string, with the possible exception of the VB-style "" for a single quotation mark (not supported by most BASICs). To insert otherwise-unprintable characters requires the use of CHR$. (One notable exception is FreeBASIC, which supports C-style escaping with OPTION ESCAPE.)

Strings can optionally be declared as being a certain length, much like C strings.

DIM c AS STRING * 1, s AS STRING

c = "char" 'everything after the first character is silently discarded
s = "string"
PRINT CHR$(34); s; " data "; c; CHR$(34)
Output:
"string data c"

Applesoft BASIC

M$ = CHR$(13) : Q$ = CHR$(34)
A$ =      "THERE ARE" + M$
A$ = A$ + "NO " + Q$ + "HERE" + Q$ + " STRINGS."
? A$

IS-BASIC

100 PRINT CHR$(34)
110 PRINT """"
120 PRINT "This is a ""quoted string""."

BASIC256

print "Hello, World."
print chr(34); "Hello, World." & chr(34)
print "Tom said," + "'The fox ran away.'"

ZX Spectrum Basic

The ZX Spectrum supports the use of CHR$(34). Alternatively, it is possible to print the doublequotes, by adding an extra pair of doublequotes:

10 REM Print some quotes
20 PRINT CHR$(34)
30 REM Print some more doublequotes
40 PRINT """"
50 REM Output the word hello enclosed in doublequotes
60 PRINT """Hello"""

uBasic/4tH

uBasic/4tH supports the inclusion of doublequotes by allowing the escape \q in almost every string literal.

Print "This is a ";Chr(Ord("\q"));"quoted string";Chr(Ord("\q"))
Print "This is a \qquoted string\q"
a := "This is a \qquoted string\q" : Print Show(a)

BBC BASIC

Quoted (literal) strings consist of 8-bit characters and support both ANSI and UTF-8 encodings; they may not contain 'control' characters (0x00 to 0x1F). The only special character is the double-quote " which must be escaped as "". There is no special representation for a single character (it is just a string of length one). 'Here strings' are not supported.

      PRINT "This is a ""quoted string"""
Output:
This is a "quoted string"

bc

The double-quote " starts a literal string which ends at the next double-quote. Thus strings can span multiple lines and cannot contain a double-qoute (there is no escaping mechanism).

Characters are just strings of length one.

Befunge

The double quote character (") enters a string literal mode, where ASCII values of characters encountered in the current instruction pointer direction up to the next quote are pushed onto the stack. Thus, any character may be used in a string except for a quote (ascii 34), which may be pushed using 57*1-. Note: since you are pushing the string onto a stack, you usually want to define the string in reverse order so that the first character is on top.

"gnirts">:#,_@

Bracmat

Strings of any length can always be surrounded by quotes. They must be surrounded by quotes if the string contains white space characters or one of the characters =.,|&:+*^'$_;{} or character sequences \D or \L. They must also be surrounded by quotes if they start with one or more characters from the set [~/#<>%@?!-. Inside strings the characters " and \ must be escaped with a backslash \. White space characters for carriage return, newline and tabulator can be expressed as \r, \n and \t, respectively. Escape sequences need not be enclosed in quotes. A string expression prepended with @ or % has no escape sequences: all characters except quotes are taken litterally. These are 10 examples of valid string expressions. (The last example is a multiline string.)

Examples:
string
"string"
stri\t-\tng\r\n
"-10"
"+10"
"{a*b}"
".,|&:+*^'$_"
"[~/#<>%@?!-"
string[~/#<>%@?!-
"str; ing
.,|&:+*^'$_

   "

C

In C, single characters are contained in single quotes.

char ch = 'z';

Strings are contained in double quotes.

char str[] = "z";

This means that 'z' and "z" are different. The former is a character while the latter is a string, an array of two characters: the letter 'z' and the string-terminator null '\0'.

C has no raw string feature (please define). C also has no built-in mechanism for expanding variables within strings.

A string can span lines. Newlines can be added via backslash escapes, and string literals are concatenated when compiled:

char lines[] = "line 1\n"
 "line 2\n"
 "line 3\n";

C can use library functions such as sprintf for doing formatted replacement within strings at run time, or preprocessor concatenation to build string literals at compile time:

#define FOO  "prefix_"##MY_SYMBOL##"_suffix"

C#

C# uses single quotes for characters and double quotes for strings just like C.

C# supports verbatim strings. These begin with @" and end with ". Verbatim quotes may contain line breaks and so verbatim strings and here-strings overlap.

string path = @"C:\Windows\System32";
string multiline = @"Line 1.
Line 2.
Line 3.";

C++

Quoting is essentially the same in C and C++.

In C++11, it is also possible to use so-called "Raw Strings":

auto strA = R"(this is
a newline-separated
raw string)";

Clojure

Character literals are prefixed by a backslash:

[\h \e \l \l \o] ; a vector of characters
\uXXXX           ; where XXXX is some hex Unicode code point
\\               ; the backslash character literal

There are also identifiers for special characters:

\space
\newline
\tab
\formfeed
\return
\backspace

Clojure strings are Java Strings, and literals are written in the same manner:

"hello world\r\n"

COBOL

Strings can be enclosed in either single quotes or double quotes. There is no difference between them.

"This is a valid string."
'As is this.'

Character literals are strings of two-digit hexadecimal numbers preceded by an x.

X"00" *> Null character
X"48656C6C6F21" *> "Hello!"

There are also figurative constants which are equivalent to certain string literals:

HIGH-VALUE HIGH-VALUES *> Equivalent to (a string of) X"FF".
LOW-VALUE  LOW-VALUES  *>  "                       "  X"00".
NULL                   *>  "                       "  X"00".
QUOTE      QUOTES      *>  "                       "  double-quote character.
SPACE      SPACES      *>  "                       "  space.
ZERO   ZEROS   ZEROES  *>  "                       "  zero.

Common Lisp

Character literals are referenced using a hash-backslash notation. Strings are arrays or sequences of characters and can be declared using double-quotes or constructed using other sequence commands.

(let ((colon #\:)
      (str "http://www.rosettacode.com/"))
   (format t "colon found at position ~d~%" (position colon str)))

D

Character literals:

char c = 'a';

Regular strings support C-style escape sequences.

auto str = "hello";   // UTF-8
auto str2 = "hello"c;  // UTF-8
auto str3 = "hello"w; // UTF-16
auto str4 = "hello"d; // UTF-32

Literal string (escape sequences are not interpreted):

auto str = `"Hello," he said.`;
auto str2 = r"\n is slash-n";

Specified delimiter string:

// Any character is allowed after the first quote;
// the string ends with that same character followed 
// by a quote.
auto str = q"$"Hello?" he enquired.$";
// If you include a newline, you get a heredoc string:
auto otherStr = q"EOS
This is part of the string.
    So is this.
EOS";

Token string:

// The contents of a token string must be valid code fragments.
auto str = q{int i = 5;};
// The contents here isn't a legal token in D, so it's an error:
auto illegal = q{@?};

Hex string:

// assigns value 'hello' to str
auto str = x"68 65 6c 6c 6f";

Delphi

var
  lChar: Char;
  lLine: string;
  lMultiLine: string;
begin
  lChar := 'a';
  lLine := 'some text';
  lMultiLine := 'some text' + #13#10 + 'on two lines';

DWScript

Strings are either single or double quote delimited, if you want to include the delimiter in the string, you just double it. Specific character codes (Unicode) can be specified via # (outside of the string).

const s1 := 'quoted "word" in string';   
const s2 := "quoted ""word"" in string"; // sames as s1, shows the doubling of the delimiter
const s2 := 'first line'#13#10'second line';   // CR+LF in the middle

Dyalect

Strings in Dyalect are double quote delimited (and characters are single quote delimited). Both support escape codes:

let c = '\u0020' //a character
let str = "A string\non several lines!\sAnd you can incorporate expressions: \(c)!"

Dyalect also supports multiline strings:

let long_str = <[first line
second line
third line]>

Multiline strings do not support escape codes.

Déjà Vu

local :s "String literal"
local :s2 "newline \n carriage return \r tab \t"
!print "backslash \\ quote \q decimal character \{8364}"
Output:
backslash \ quote " decimal character €

E

E has three sorts of quotes: strings, characters, and quasiliterals.

'T'                           # character
"The quick brown fox"         # string
`The $kind brown fox`         # "simple" quasiliteral
term`the($adjectives*, fox)`  # "term" quasiliteral

Strings and characters use syntax similar to Java; double and single quotes, respectively, and common backslash escapes.

Quasiliterals are a user-extensible mechanism for writing any type of object "literally" in source, with "holes" for interpolation or pattern-matching. The fourth example above represents a Term object (terms are a tree language like XML or JSON), with the items from the variable adjectives spliced in.

Quasiliterals can be used for strings as well. The third example above is the built-in simple interpolator, which also supports pattern matching. There is also a regular-expression quasi-pattern:

? if ("<abc,def>" =~ `<@a,@b>`) { [a, b] } else { null }
# value: ["abc", "def"]

? if (" >abc, def< " =~ rx`\W*(@a\w+)\W+(@b\w+)\W*`) { [a, b] } else { null }
# value: ["abc", "def"]

EasyLang

Strings are always enclosed in double quotes ("). Unicode is also supported.

print "EasyLang"
print "简"

Ela

Ela has both characters:

c = 'c'

and strings:

str = "Hello, world!"

Both support C-style escape codes:

c = '\t'
str = "first line\nsecond line\nthird line"

Also Ela supports verbatim strings with the following syntax:

vs = <[This is a
  verbatim string]>

Elena

ELENA 4.x :

   var c := $65; // character
   var s := "some text"; // UTF-8 literal
   var w := "some wide text"w; // UTF-16 literal
   var s2 := "text with ""quotes"" and 
two lines";

Elixir

String

Strings are between double quotes; they're represented internally as utf-8 encoded bytes and support interpolation.

IO.puts "Begin String \n============"
str = "string"
str |> is_binary # true

While internally represented as a sequence of bytes, the String module splits the codepoints into strings.

str |> String.codepoints

The bytes can be accessed by appending a null byte to the string

str <> <<0>>

Strings can be evaluated using ? before a character in the command line or in a string, then evaluating the string

?a # 97
Code.eval_string("?b") # 98
Code.eval_string("?ł") # 322

Char Lists

Char lists are simply lists of characters. Elixir will attempt to convert number values to characters if a string could be formed from the values. Char lists represent characters as single quotes and still allow for interpolation.

IO.inspect "Begin Char List \n============="
[115, 116, 114, 105, 110, 103]
ch = "hi"
'string #{ch}'

Again, since 0 cannot be rendered as a character, adding it to a char list will return the char list

'string #{ch}'++[0]
Output:

Begin String
============
"string"
true
["s", "t", "r", "i", "n", "g"]
<<115, 116, 114, 105, 110, 103, 0>>
97
98
322
Begin Char List
===============
'string'
'string hi'
[115, 116, 114, 105, 110, 103, 32, 104, 105, 0]

Emacs Lisp

Strings

The only string literal is a double-quote

"This is a string."

Backslash gives various special characters similar to C, such as \n for newline and \" for a literal double-quote. \\ is a literal backslash. See "Syntax for Strings" in the elisp manual.

Characters

A character is an integer in current Emacs. (In the past character was a separate type.) ? is the read syntax.

?z    ;=> 122
?\n   ;=> 10

See "Basic Char Syntax" in the elisp manual.

Erlang

Erlang strings are lists containing integer values within the range of the ASCII or (depending on version and settings) Unicode characters.

"This is a string".
[$T,$h,$i,$s,$ ,$a,$ ,$s,$t,$r,$i,$n,$g,$,,$ ,$t,$o,$o].

Characters are represented either as literals (above) or integer values.

97 == $a. % => true

With the string syntax, characters can be escaped with \.

"\"The quick brown fox jumps over the lazy dog.\"".

F#

let n= 'N'
let i="Name=\"Nigel Galloway\"\n"
let g= @"Name=""Nigel Galloway""\n"
let e="Nigel
Galloway"
let l= """Name="Nigel Galloway"\n"""
printfn "%c\n%s\n%s\n%s\n%s" n i g e l
Output:
N
Name="Nigel Galloway"

Name="Nigel Galloway"\n
Nigel
Galloway
Name="Nigel Galloway"\n

Factor

A basic character:

CHAR: a

Characters are Unicode code points (integers in the range [0-2,097,152]).

CHAR: is a parsing word that takes a literal character, escape code, or Unicode code point name and adds a Unicode code point to the parse tree.

CHAR: x                       ! 120
CHAR: \u000032                ! 50
CHAR: \u{exclamation-mark}    ! 33
CHAR: exclamation-mark        ! 33
CHAR: ugaritic-letter-samka   ! 66450

Strings are represented as fixed-size mutable sequences of Unicode code points.

A basic string:

"Hello, world!"

We can take a look under the hood:

"Hello, world!" { } like ! { 72 101 108 108 111 44 32 119 111 114 108 100 33 }

Both CHAR: and strings support the following escape codes:

Escape code Meaning
\\ \
\s a space
\t a tab
\n a newline
\r a carriage return
\b a backspace (ASCII 8)
\v a vertical tab (ASCII 11)
\f a form feed (ASCII 12)
\0 a null byte (ASCII 0)
\e escape (ASCII 27)
\" "
\xxx The Unicode code point with hexadecimal number xxx
\uxxxxxx The Unicode code point with hexadecimal number xxxxxx
\u{name} The Unicode code point named name

Some examples of strings with escape codes:

"Line one\nLine two" print
Output:
Line one
Line two

Putting quotation marks into a string:

"\"Hello,\" she said." print
Output:
"Hello," she said.

Strings can span multiple lines. Newlines are inserted where they occur in the literal.

"2\u{superscript-two} = 4
2\u{superscript-three} = 8
2\u{superscript-four} = 16" print
Output:
2² = 4
2³ = 8
2⁴ = 16

The multiline vocabulary provides support for verbatim strings and here-strings.

A verbatim string:

USE: multiline
[[ escape codes \t are literal \\ in here
but newlines \u{plus-minus-sign} are still
inserted " for each line the string \" spans.]] print
Output:
escape codes \t are literal \\ in here
but newlines \u{plus-minus-sign} are still
inserted " for each line the string \" spans.

Note that the space after [[ is necessary for the Factor parser to recognize it as a word. " is one of very few special cases where this is not necessary. The space will not be counted as part of the string.

A here-string:

USE: multiline
HEREDOC: END
Everything between the line above
         and the final line (a user-defined token)
      is parsed into a string where whitespace
  is         significant.
END
print
Output:
Everything between the line above
         and the final line (a user-defined token)
      is parsed into a string where whitespace
  is         significant.

STRING: is similar to HEREDOC: except instead of immediately placing the string on the data stack, it defines a word that places the string on the data stack when called.

USE: multiline
STRING: random-stuff
ABC
123
    "x y z
;
random-stuff print
Output:
ABC
123
    "x y z

Finally, the interpolate vocabulary provides support for interpolating lexical variables, dynamic variables, and data stack values into strings.

USING: interpolate locals namespaces ;

"Sally" "name" set
"bicycle"
"home"

[let

"crying" :> a

[I ${name} crashed her ${1}. Her ${1} broke.
${name} ran ${} ${a}.
I]

]
Output:
Sally crashed her bicycle. Her bicycle broke.
Sally ran home crying.

${} consumes values from the stack. With a number n inside, you can reference (and re-reference!) the data stack value n places from the top of the data stack.

Forth

In the interpreter:

char c   emit
s" string"   type

In the compiler:

: main
  [char] c   emit
  s" string"   type ;

Strings may contain any printable character except a double quote, and may not span multiple lines. Strings are done via the word S" which parses ahead for a terminal quote. The space directly after S" is thus not included in the string.

Works with: GNU Forth

GNU Forth has a prefix syntax for character literals, and another string literal word S\" which allows escaped characters, similar to C.

'c emit
s\" hello\nthere!"

Fortran

First Fortran (1958) did not offer any means to manipulate text except via the H (for Hollerith) code in FORMAT statements of the form nH where n was an integer that counted the exact numbers of characters following the H, any characters, that constituted the text literal. Miscounts would cause a syntax error, if you were lucky. This would be used for output to annotate the numbers, but consider the following:

      DIMENSION ATWT(12)
      PRINT 1
    1 FORMAT (12HElement Name,F9.4)
      DO 10 I = 1,12
        READ  1,ATWT(I)
   10   PRINT 1,ATWT(I)
      END

Evidently, the syntax highlighter here does not recognise the Hollerith style usage. Nor do some compilers, even if in its original home within FORMAT statements.

The first PRINT statement writes out a heading, here with lower case letters as an anachronism. Then the loop reads a deck of cards containing the name of an element and its atomic weight into an array ATWT, but the special feature is that the first twelve characters of each card replace the text in the FORMAT statement, and thus the following PRINT statement shows the name of the element followed by its atomic weight as just read.

Fortran IV introduced a text literal, specified within apostrophes, with two apostrophes in a row indicating an apostrophe within the text. Later, either an apostrophe or a double quote could be used to start a text string (and the same one must be used to end it) so that if one or the other were desired within a text literal, the other could be used as its delimiters. If both were desired, then there would be no escape from doubling for one. Because spaces are significant within text literals, a long text literal continued on the next line would have the contents of column seven of the continuation line immediately following the contents of column 72 of the continued line - except that (for some compilers reading disc files) if such lines did not extend to column 72 (because trailing spaces were trimmed from the records) rather less text would be defined. So, even though this is in fixed-format (or card image) style, again misinterpreted by the syntax highlighter,

      BLAH = "
     1Stuff"

might be the equivalent of only BLAH = "Stuff" instead of defining a text literal with many leading spaces. F90 formalised an opportunity for free-format source files; many compilers had also allowed usage beyond column 72.

Within the text literal, any character whatever may be supplied as text grist, according to the encodement recognised by the card reader as this was a fixed-format file - cards have an actual physical size. This applied also to source text held in disc files, as they were either fixed-size records or, for variable-length records, records had a length specification and the record content was not involved. Variable-length records were good for omitting the storage of the trailing spaces on each line, except that the sequence numbers were at the end of the line! In this case they might be omitted (unlike a card deck, a disc file's records are not going to be dropped) or there may be special provision for them to be at the start of each line with the source text's column one staring in column nine of the record. But, for the likes of paper tape, the question "How long is a record?" has no natural answer, and record endings were marked by a special symbol. Such a symbol (or symbol sequence) could not appear within a record, such as within a text literal and be taken as a part of the text. This style has been followed by the ASCII world, with variously CR, CRLF, LFCR and CR sequences being used to mark end-of-record. Such characters cannot be placed within a text literal, but the CHAR(n) function makes them available in character expressions. Some compilers however corrupt the "literal" nature of text literals by allowing escape sequences to do so, usually in the style popularised by C, thus \n, and consequently, \\ should a single \ be desired.

Some examples, supposing that TEXT is a CHARACTER variable.

      TEXT = 'That''s right!'            !Only apostrophes as delimiters. Doubling required.
      TEXT = "That's right!"             !Chose quotes, so that apostrophes may be used freely.
      TEXT = "He said ""That's right!""" !Give in, and use quotes for a "quoted string" source style.
      TEXT = 'He said "That''s right!"'  !Though one may dabble in inconsistency.
      TEXT = 23HHe said "That's right!"  !Some later compilers allowed Hollerith to escape from FORMAT.

A similar syntax enables the specification of hexadecimal, octal or binary sequences, as in X = Z"01FE" for hexadecimal (the "H" code already being used for "Hollerith" even if the H-usage is not supported by the compiler) but this is for numerical values, not text strings. While one could mess about with EQUIVALENCE statements, numbers fill up from the right while text strings fill from the left and there would be "endian" issues as well, so it is probably not worth the bother. Just use the CHAR function in an expression, as in

      TEXT = "That's"//CHAR(10)//"right!" !For an ASCII linefeed (or newline) character.

Which may or may not be acted upon by the output device. A lineprinter probably would ignore a linefeed character but a teletype would not - it would roll the printing carriage one line up without returning to the column one position, thus the usage LFCR (or CRLF) to add the carriage return action. Some systems regard the LF as also implying a CR and for these the notation \n for "newline" is mnemonic even though there is no "newline" character code in ASCII - though there is in EBCDIC. Display screens do not handle glyph construction via overprinting though teletypes (and lineprinters) do. Similarly, a display screen may or may not start a new screen with a formfeed character and a lineprinter won't start a new page - at least if attached to a mainframe computer.

FreeBASIC

Print "Hello, World."
Print Chr(34); "Hello, World." & Chr(34) 

Print "Tom said, ""The fox ran away."""
Print "Tom said," + "'The fox ran away.'"

friendly interactive shell

echo Quotes are optional in most cases.
echo
echo 'But they are when using either of these characters (or whitespace):'
echo '# $ % ^ & * ( ) { } ; \' " \\ < > ?'
echo
echo Single quotes only interpolate \\ and \' sequences.
echo '\In \other \cases, \backslashes \are \preserved \literally.'
echo
set something variable
echo "Double quotes interpolates \\, \" and \$ sequences and $something accesses."

FurryScript

A name literal starts with ` and is one word long; it functions like a string.

A normal string literal uses angle brackets (< and >) around it. You can have additional < > pairs inside (which can nest to any level) in order to represent subroutine calls (they are not called where the string literal appears; they are called only once it is processed).

A story text uses {|| and ||} around it, and can contain any text, with no escapes supported.

All three kinds are string literals.

FutureBasic

@"Hello, world!"

GAP

IsChar('a');
# true
IsString("abc");
# true
IsString('a');  
# false
IsChar("a");   
# false

gecho

'a outascii

Just one character.

'yo...dawg. print

A string.

Go

See the language specification sections on rune literals and string literals.

In Go, character literals are called "rune literals" and can be any single valid Unicode code point. They are written as an integer value or as text within single quotes.

ch := 'z'
ch = 122          // or 0x7a or 0172 or any other integer literal
ch = '\x7a'       // \x{2*hex}
ch = '\u007a'     // \u{4*hex}
ch = '\U0000007a' // \U{8*hex}
ch = '\172'       // \{3*octal}

A rune literal results in an untyped integer. When used in a typed constant or stored in a variable, usually the type is either byte or rune to distinguish character values from integer values. These are aliases for uint8 and int32 respectively, but like other integer types in Go, they are distinct and require an explicate cast.

ch := 'z'          // ch is type rune (an int32 type)
var r rune = 'z'   // r is type rune
var b byte = 'z'   // b is type byte (an uint8 type)
b2 := byte('z')    // equivalent to b
const z = 'z'      // z is untyped, it may be freely assigned or used in any integer expression
b = z
r = z
ch2 := z           // equivalent to ch (type rune)
var i int = z
const c byte = 'z' // c is a typed constant
b = c
r = rune(c)
i = int(c)
b3 := c            // equivalent to b

Strings literals are are either interpreted or raw.

Interpreted string literals are contained in double quotes. They may not contain newlines but may contain backslash escapes.

str := "z"
str = "\u007a"
str = "two\nlines"

This means that 'z' and "z" are different. The former is a character while the latter is a string.

Unicode may be included in the string literals. They will be encoded in UTF-8.

str := "日本語"

Raw string literals are contained within back quotes. They may contain any character except a back quote. Backslashes have no special meaning.

`\n` == "\\n"

Raw string literals, unlike regular string literals, may also span multiple lines. The newline is included in the string (but not any '\r' characters):

`abc
def` == "abc\ndef", // never "abc\r\ndef" even if the source file contains CR+LF line endings

Go raw string literals serve the purpose of here-strings in other languages. There is no variable expansion in either kind of string literal (the Go text/template package provides something like variable expansion).

Groovy

In Groovy, unlike in Java, a String literal is delimited with single quotes (apostrophes(')).

def string = 'Able was I'

There is a double quote (quotation mark(")) delimited syntax in Groovy, but it represents an expression construct called a GString (I know, I know). Inside of a GString, sub-expression substitution of the form ${subexpression} may take place. Thus the following results:

def gString = "${string} ere I saw Elba"

println gString

//Outputs:
//Able was I ere I saw Elba

UNIX Shell command line users should recognize these forms of syntax as strong ('-delimited) and weak ("-delimited) quoting. And like UNIX Shell weak quoting syntax, the evaluated subexpression part of the GString syntax loses its special meaning when preceded by a backslash (\):

def gString2 = "1 + 1 = ${1 + 1}"
assert gString2 == '1 + 1 = 2'

def gString3 = "1 + 1 = \${1 + 1}"
assert gString3 == '1 + 1 = ${1 + 1}'

Groovy also supports multi-line String literals and multi-line GString expressions.

def multiLineString = '''
A man
A plan
A canal
'''

def multiLineGString = """
${multiLineString.trim()}:
Panama!
"""

println multiLineGString

//Outputs:
//
//A man
//A plan
//A canal:
//Panama!
//

UNIX Shell programmers should recognize these forms of syntax as similar in function to the strong and weak forms of Here Document syntax.

Both String literals and GString expressions support a number of special characters (usually non-printable characters) which consist of a single character preceded by a backslash (hence \X), or a 4-hexadecimal-digit UNICODE encoding preceded by a backslash and a lowercase "u" (hence \uXXXX).

One of these special characters is the backslash itself, denoted with in a String or GString as \\. This actually interferes with regular expression syntax in which literal backslashes play various important regular-expression-specific roles. Thus it can become quite onerous to write regular expressions using String or GString quoting syntax, since every regex backslash would have to be written as \\.

However, Groovy has a special GString syntax that uses slash (/) as a GString delimiter rather that quote ("). In this special syntax, most backslash usages that would require a double backslash in a regular String or GString require only a single backslash (\). This does not create a "regular expression object" (there is not such a thing in Groovy); however, it does evaluate to form a "regular expression ready" String, as demonstrated in the following:

def regexString = /(\[[Tt]itle\]|\[[Ss]ubject\])${10 * 5}/

assert regexString == '(\\[[Tt]itle\\]|\\[[Ss]ubject\\])50'

Javascript users (and others) will recognize the roots of this "regex-ready" syntax as a feature in their own language.

Since apostrophe is used to delimit String literals, that delimiter syntax is not available, as it is in Java, to denote single character literals (type char or Character). However, single character string literals can be converted to character literals by casting. Shown in the examples below are casting using the as operator, Java-style parenthetical casting, and forced coercion in the intialization of a variable of type char or Character.

assert 'a' instanceof String
assert ('a' as char) instanceof Character
assert ((char)'a') instanceof Character

char x = 'a'
assert x instanceof Character
Character y = 'b'
assert y instanceof Character && (x+1 == y)

As in Java, backslash is also used to mask a string delimiter. Thus the following two assignments represent strings containing a single quote and a single apostrophe respectively

def quote = "\""
def apostrophe = '\''

Of course, if you are not using GString subexpression evaluation, you can just use apostrophe delimiters to contain a quote, or quote delimiters to contain an apostrophe.

def quote2 = '"'
def apostrophe2 = "'"
assert quote == quote2
assert apostrophe == apostrophe2

Haskell

language support

Characters use single quotes, strings use double quotes. Both allow Unicode. Escape sequences start with a backslash. There are no verbatim strings, no here-strings, and no expansion of variables in strings.

Strings may be split across lines, even indented, using the 'gap' syntax:

"abcdef" == "abc\
            \def"

"abc\ndef" == "abc\n\
              \def"

You can also use \& which expands into nothing (but can be useful to interrupt another escape sequence).

The Haskell 98 Report section Character and String Literals has more information.

using raw-strings-qq package

using raw-strings-qq package:

{-# LANGUAGE QuasiQuotes #-}
import Text.RawString.QQ

"abc\ndef" == [r|abc
def|]

HicEst

HicEst makes no distinction between single characters and strings. One can use single quotes, or double quotes, or most non-standard characters.

CHARACTER c1='A', c2="B", c3=&C&
CHARACTER str1='single quotes', str2="double quotes", str3*100

str3 = % delimit "Nested 'strings' " if needed %

A null character CHAR(0) is printed as " ", displayed as "." in dialogs, but ends the string in Windows controls such as StatusBar or ClipBoard

str3 = 'a string' // CHAR(0) // "may contain" // $CRLF // ~ any character ~

Named literal constants in HicEst:

$TAB == CHAR(9)               ! evaluates to 1 (true)
$LF  == CHAR(10)
$CR  == CHAR(13)
$CRLF == CHAR(13) // CHAR(10) ! concatenation

Icon and Unicon

Below is a little program to demonstrate string literals.

procedure main()

   # strings are variable length are not NUL terminated
   # at this time there is no support for unicode or multi-byte charactersets

   c1 := 'aaab'                                          # not a string - cset
   s1 := "aaab"                                          # string
   s2 := "\"aaab\b\d\e\f\n\l\n\r\t\v\'\"\\\000\x00\^c"   # with escapes and imbedded zero

   # no native variable substitution, a printf library function is available in the IPL

   every x := c1|s1|s2 do                                # show them
      write(" size=",*x,", type=", type(x),", value=", image(x))
end
Output:
  size=2, type=cset, value='ab'
  size=4, type=string, value="aaab"
  size=21, type=string, value="\"aaab\b\d\e\f\n\n\n\r\t\v'\"\\\x00\x00\x03"

IDL

The single and double quotes are fairly interchangeable allowing one to use whichever isn't to be quoted (though single-quotes seem more well-behaved around integers in strings). Thus the following are both valid character-constant assignments:

a = " that's a string "
b = ' a "string" is this '

In a pinch, a character constant doesn't absolutely have to be terminated, rendering the following valid:

a = " that's a string

Duplicating either of them quotes them. Thus the following contains three single quotes and no double-quotes:

a = ' that''s a string 
print,a
;==>  that's a string

Things in quotes are not expanded. To get to the content of a variable, leave it unquoted:

b = 'hello'
a = b+' world
print,a
;==>  hello world

Single-quoted strings of valid hex or octal digits will be expanded if followed by "x" or "o":

print,'777'x
;==>   1911
print,'777'o
;==>    511
print,'777'
;==>    777

so will be unterminated double-quoted strings if they represent valid octal numbers:

print,"777
;==>    511
print,"877
;==>    877

Note that this renders the following false (common trip-up for IDL newbies):

a = "0"
;==> Syntax error.

...because the number zero indicates that an octal number follows, but the second double-quote is not a valid octal digit.

Byte-arrays that are converted into strings are converted to the ascii-characters represented by the bytes. E.g.

crlf = string([13b,10b])

Inform 7

String literals are enclosed in double quotes. These may include raw line breaks, or expressions to be substituted enclosed in square brackets.

Home is a room. The description is "This is where you live...

...with your [number of animals in Home] pet[s]."

Single quotes in a string are translated to double quotes when they occur outside of a word: the string literal

"'That's nice,' said the captain."

will print as

"That's nice," said the captain.

Raw linebreak must be double -- single linebreaks will be collapsed unless explicitly marked with `[line break]`. In addition, leading whitespace is stripped from each line. This:

"
\
 \
  \
   \"

will print as:

\\\\

while this:

"
[line break]\
[line break] \
[line break]  \
[line break]   \"

will insert line breaks and preserve the following whitespace, printing as:

\
 \
  \
   \

There are no character literals: phrases that manipulate characters pass them as single-character strings.

J

Like C, J treats strings as lists of characters. Character literals are enclosed in single quotes, and there is no interpolation. Therefore, the only "escape" character neccessary is the single-quote itself, and within a character literal is represented by a pair of adjacent single quotes (much like in C, where within a character literal, a slash is represented by a pair of adjacent slashes).

Examples:

'x'                   NB.  Scalar character
'string'              NB.  List of characters, i.e. a string
'can''t get simpler'  NB.  Embedded single-quote

Like VB, J can include newlines and other special characters in literals with concatentation. Also like VB, J comes with certain constants predefined for some characters:

'Here is line 1',LF,'and line two'

'On a mac, you need',CR,'a carriage return'

'And on windows, ',CRLF,'you need both'

TAB,TAB,TAB,'Everyone loves tabs!'

These constants are simply names assigned to selections from the ASCII alphabet. That is, the standard library executes lines like this:

CR   =:  13 { a.
LF   =:  10 { a.
CRLF =:  CR,LF     NB.  Or just  10 13 { a.
TAB  =:  9 { a.

Since these constants are nothing special, it can be seen that any variable can be similarly included in a literal:

NAME =:  'John Q. Public'
'Hello, ',NAME,' you may have already won $1,000,000'

For multiline literals, you may define an explicit noun, which is terminated by a lone )

template =: noun define
Hello, NAME.  

My name is SHYSTER, and I'm here to tell
you that you my have already won $AMOUNT!!

To collect your winnings, please send $PAYMENT
to ADDRESS.
)

Simple substitution is most easily effected by using loading a standard script:

load 'strings'

name    =:  'John Q. Public'
shyster =:  'Ed McMahon'
amount  =:  1e6
payment =:  2 * amount
address =:  'Publisher''s Clearing House'
 
targets =:  ;:   'NAME SHYSTER AMOUNT PAYMENT ADDRESS'
sources =:  ":&.> name;shyster;amount;payment;address

message =: template rplc targets,.sources

While C-like interpolation can be effected with another:

   load 'printf'
   
   'This should look %d%% familiar \nto programmers of %s.' sprintf 99;'C'
This should look 99% familiar 
to programmers of C.

Java

  char a = 'a';  // prints as: a
  String b = "abc";  // prints as: abc
  char doubleQuote = '"';  // prints as: "
  char singleQuote = '\'';  // prints as: '
  String singleQuotes = "''";  // prints as: ''
  String doubleQuotes = "\"\"";  // prints as: ""

Null characters ('\0') are printed as spaces in Java. They will not terminate a String as they would in C or C++. So, the String "this \0is \0a \0test" will print like this:

this  is  a  test

JavaScript

A JavaScript string is a sequence of zero or more characters enclosed in either 'single quotes' or "double quotes". Neither form prevents escape sequences: "\n" and '\n' are both strings of length 1. There is no variable interpolation.

Unicode characters can be entered as literals or as 4 character hexadecimal escapes. The following expressions are equivalent:

(function () {
    return "αβγδ 中间来点中文 🐫 אבגד"
})();


(function() {
    return "\u03b1\u03b2\u03b3\u03b4 \u4e2d\u95f4\u6765\u70b9\u4e2d\u6587 \ud83d\udc2b \u05d0\u05d1\u05d2\u05d3";
})();

Note that in the case of the Emoji character above, where more than 4 hexadecimal characters are needed, ES5 requires us to separately write a pair of surrogate halves, and the String.length of such characters is 2.

ES6 introduces Unicode code point escapes such as

'\u{2F804}'

allowing direct escaping of code points up to 0x10FFFF.


ES6 also introduces template literals, which are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. Template literals are enclosed by the backtick (` `) (grave accent) character instead of double or single quotes.

const multiLine = `string text line 1
string text line 2`
const expression = `expressions are also supported, using \$\{\}: ${multiLine}`

console.log(expression)
Output:
expressions are also supported, using ${}: string text line 1
string text line 2

jq

jq supports all JSON types, including JSON strings; jq also supports "string interpolation".

The rules for constructing JSON string literals are explained elsewhere (notably at json.org), so here we'll focus on "string interpolation" -- a technique for creating JSON strings programmatically using string literals, much like ruby's "#{...}", for example. The twist is that the string literal for specifying string interpolation is (by design) not itself a valid JSON string.

Suppose that:

  • s is (or is a reference to) a JSON entity (e.g. a string or a number), and
  • we wish to create a JSON string that is some combination of JSON strings and the string value of s, for example: "The value of s is " + (s|tostring).

jq allows the shorthand: "The value of s is \(s)", and in general, arbitrarily many such interpolations may be made.

JSON

A JSON string literal is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. Unicode code points can be represented as a hexadecimal escape sequence, like "\u002F" which is the same as "/". Multi-line strings are not supported.

Julia

Concatenation:

greet = "Hello"
whom = "world"
greet * ", " * whom * "."

Interpolation:

"$greet, $whom."

Both will output:

Hello, world.

Triple-quoted strings

str = """Hello,
        world.
         """

print(str)

Will output:

Hello,
world.

Kotlin

Kotlin supports two kinds of string literals (UTF-16 encoded):

  • escaped string literals, enclosed in double-quotes, which can contain 'escaped characters'.
  • raw string literals, enclosed in triple double-quotes, which ignore escaping but can contain new lines.


The language also supports character literals - a single UTF-16 character (including an escaped character) enclosed in single quotes.

Here are some examples of these :

// version 1.0.6

fun main(args: Array<String>) {
    val cl = 'a'          // character literal - can contain escaped character
    val esl = "abc\ndef"  // escaped string literal - can contain escaped character(s)
    val rsl = """
              This is a raw string literal   
              which does not treat escaped characters 
              (\t, \b, \n, \r, \', \", \\, \$ and \u)
              specially and can contain new lines.

              "Quotes" or doubled ""quotes"" can
              be included without problem but not
              tripled quotes.
              """
    val msl = """
              |Leading whitespace can be removed from a raw
              |string literal by including
              |a margin prefix ('|' is the default)
              |in combination with the trimMargin function.
              """.trimMargin()
    println(cl)
    println(esl)
    println(rsl)
    println(msl)
}
Output:
a
abc
def

              This is a raw string literal
              which does not treat escaped characters
              (\t, \b, \n, \r, \', \", \\, \$ and \u)
              specially and can contain new lines.

              "Quotes" or doubled ""quotes"" can
              be included without problem but not
              tripled quotes.

Leading whitepace can be removed from a raw
string literal by including
a margin ('|' is the default)
in combination with the trimMargin function.

LabVIEW

LabVIEW is a graphical language so it uses graphical string delimiters. No escaping is needed.
This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.

Lasso

All strings in Lasso are Unicode strings. This means that a string can contain any of the characters available in Unicode. Lasso supports two kinds of string literals: quoted and ticked. Quoted strings can contain escape sequences, while ticked strings cannot. Both quoted and ticked string literals can contain line breaks and they both return same type of string object. [1]

Quoted Strings

'I\'m a 2" string\n'
"I'm a 2\" string\n"

Ticked Strings

In the below example here \n would not be a line feed, it represents a backslash and n.

`I'm also a 2" string\n`

LaTeX

Since LaTeX is a markup language rather than a programming language, quotes are displayed rather than interpreted. However, quotes do deserve special mention in LaTeX. Opening (left) quotes are denoted with backquotes and closing (right) quotes are denoted with quotes. Single quotes use a single symbol and double quotes use double symbols. For example, to typeset 'a' is for "apple" in LaTeX, one would type

\documentclass{minimal}
\begin{document}
`a' is for ``apple"
\end{document}

One common mistake is to use the same symbol for opening and closing quotes, which results in the one of the quotes being backward in the output. Another common mistake is to use a double quote symbol in the input file rather than two single quotes in order to produce a double quote in the output.

Liberty BASIC

'Liberty BASIC does not support escape characters within literal strings.
print "Quotation mark:"
print chr$(34)
print

'Print literal string
print "Hello, World."
'Print literal string displaying quotation marks.
print chr$(34) + "Hello, World." + chr$(34)

Lingo

  • Lingo only supports single quotes for string literals. Single quotes inside string literals have to be replaced by "&QUOTE&":
str = "Hello "&QUOTE&"world!"&QUOTE
put str
-- "Hello "world!""
  • Lingo does not support heredoc syntax, but only multiline string literals by using the line continuation character "\":
str = "This is the first line.\
This is the second line.\
This is the third line."
  • Lingo does not support automatic variable expansion in strings. But the function value() can be used to expand template strings in the current context:
template = QUOTE&"Milliseconds since last reboot: "&QUOTE&"&_system.milliseconds"

-- expand template in current context
str = value(template)
put str
-- "Milliseconds since last reboot: 20077664"

Lisaac

Characters:

c1 := 'a';
c2 := '\n'; // newline
c3 := '\''; // quote
c4 := '\101o'; // octal
c5 := '\10\'; // decimal
c6 := '\0Ah\'; // hexadecimal
c7 := '\10010110b\'; // binary

Strings:

s1 := "this is a\nsample"; // newline
s2 := "\""; // double quote
s3 := "abc\
      \xyz"; // "abcxyz", cut the gap

LiveCode

LiveCode has only one string representation using quotes. Characters are accessed through chunk expressions, specifically char. Some special characters are built-in constants such as quote, space, comma, cr, return. There is no support for escaping characters or multiline literals.

put "Literal string"  -- Literal string
put char 1 of "Literal string"  -- L
put char 1 to 7 of "Literal string"  -- Literal
put word 1 of "Literal string"  -- Literal
put quote & "string" & quote -- "string"

Logo does not have a string or character type that is separate from its symbol type ("word"). A literal word is specified by prefixing a double-quote character. Reserved and delimiting characters, ()[];~+-*/\=<>| and newline, may be used if preceded by a backslash. Alternatively, the string may be wrapped in vertical bars, in which case only backslash and vertical bar need be escaped.

print "Hello\,\ world
print "|Hello, world|

Lua

Strings can be enclosed using singlequotes or doublequotes. Having two different types of quotation symbols enables either of the symbols to be embedded within a string enclosed with the other symbol.

singlequotestring = 'can contain "double quotes"'
doublequotestring = "can contain 'single quotes'"
longstring = [[can contain
               newlines]]
longstring2 = [==[ can contain [[ other ]=] longstring " and ' string [===[ qualifiers]==]

Note that interpolation of variables names within a string does not take place. However, interpolation of literal characters escape sequences does occur, irrespective of whether singlequote or doublequote enclosures are being used.

M2000 Interpreter

Print "Hello {World}"
Print {Hello "World"}
Report {Multiline String
      2nd line
      }
Print """Hello There"""={"Hello There"}
Print Quote$("Hello There")={"Hello There"}

M4

The quoting characters are ` and ', but can be changed by the changequote macro:

`this is quoted string'
changequote(`[',`]')dnl
[this is a quoted string]

Maple

There is no separate character type in Maple; a character is just a string of length equal to 1.

> "foobar";
                                "foobar"

> "foo\nbar"; # string with a newline
"foo
    bar"

> "c"; # a character
                                  "c"

Note that adjacent strings in the input (separated only by white-space) are concatenated automatically by the parser.

> "foo"   "bar";
                                "foobar"

Since variable names are not distinguished lexically from other text (such as by using a "$" prefix, as in some shells), Maple does not do any kind of variable expansion inside strings.

Mathematica/Wolfram Language

There is no character type in Mathematica, only string type.
"c";          // String (result: "c")
"\n";         // String (result: newline character)

MATLAB

Strings start and end with single quotes, the escape sequence for a single quote with in a string, is the use of two consequtive single quotes

  
    s1 = 'abcd'   % simple string
    s2 = 'ab''cd'   % string containing a single quote
Output:
>>     s1 = 'abcd'   % simple string
s1 = abcd
>>     s2 = 'ab''cd'   % string containing a single quote
s2 = ab'cd

Maxima

/* A string */
"The quick brown fox jumps over the lazy dog";

/* A character - just a one character string */
"a"

Metafont

In Metafont there's no difference between a single character string and a single character. Moreover, the double quotes (which delimites a string) cannot be inserted directly into a string; for this reason, the basic Metafont macro set defines

string ditto; ditto = char 34;

i.e. a string which is the single character having ASCII code 34 ("). Macro or variables expansion inside a string block is inhibited.

message "You've said: " & ditto & "Good bye!" & ditto & ".";

ML/I

ML/I treats all input and programs as character streams. Strings do not have to be quoted; they are taken 'as is'. If one wishes to ensure that a string is taken literally (i.e. not evaluated), it is enclosed in literal brackets. There are no predefined literal brackets; the programmer can define anything suitable, usually by setting up a matched text skip, using the MCSKIP operation macro. By convention, the pair <> is used for literal brackets, unless this clashes in the case of a particular processing task.

Input

MCSKIP "WITH" NL
"" Literals/String
MCINS %.
MCSKIP MT,<>
"" Demonstration of literal string
MCDEF Bob AS Alice
"" The following two lines both mention Bob. The first line is
"" evaluated, but the second is surrounded by literal brackets and is not
"" evaluated.
This is the first mention of Bob
<and here we mention Bob again>

Output

This is the first mention of Alice
and here we mention Bob again

MIPS Assembly

Works with: [ARMIPS]

Strings are specified using single or double quotes. The assembler will convert each letter of the string into its ASCII equivalent during the assembly process. Therefore, all of the following statements have the same effect:

li a0,'A'
li a0,0x41
li a0,65
li a0,0b01000001

This means that you can do compile-time "character addition/subtraction" and the like, to better communicate why your code is doing what it's doing.

;print 0 if $t0 if even, 1 if $t0 is odd

andi t0,t0,1    ;clear all but bit 1. This tells us if $t0 is odd or even.
addiu t0,"0"    ;add ASCII 0 (0x30) to $t0
jal PrintChar   ;implementation-defined print routine that prints the ASCII value of $t0 to the screen.

ASCII strings use .byte for declaration. Control codes are implemented by strategically placing commas and their numeric values outside of quotation marks, like so:

MyString:
.byte "Hello World!",13,10,0    ;carriage return, line feed, null terminator
.align 4                        ;pads to the next 4 byte-boundary

As with most RISC CPUs, alignment is a must, especially when working with ASCII strings. ARMIPS doesn't provide alignment automatically, but it does have the .align directive to provide sufficient padding (if necessary) to ensure everything after your string is properly aligned. If it was already aligned, the directive will do nothing rather than burn the bytes, meaning that you don't have to take the time to count how long your string is. There's no memory wasted by dropping a .align after every piece of byte-length data, so might as well.


Modula-3

Characters in Modula-3 use single quotes.

VAR char: CHAR := 'a';

Strings in Modula-3 use double quotes.

VAR str: TEXT := "foo";

TEXT is the string type in Modula-3. Characters can be stored in an array and then converted to type TEXT using the function Text.FromChars in the Text module.

Strings (of type TEXT) can be converted into an array of characters using the function Text.SetChars.

VAR str: TEXT := "Foo";
VAR chrarray: ARRAY [1..3] OF CHAR;

Text.SetChars(chrarray, str);
(* chrarray now has the value ['F', 'o', 'o'] *)

MUMPS

All strings are delimited by the double quotes character. But you can escape the double quotes to add a double quotes character to a string.

USER>SET S1="ABC"
 
USER>SET S2="""DEF"""
 
USER>SET S3="""GHI"
 
USER>W S1
ABC
USER>W S2
"DEF"
USER>W S3
"GHI

Nemerle

Character literals are enclosed in single quotes. Regular strings are enclosed in double quotes, and use \ to delimit special characters, whitespace similar to C. A @ preceding the double quotes indicates a literal string. A $ preceding the double quote indicates string interpolation, identifiers prefixed with $ inside the string literal will be replaced with their value. Nemerle also has a recursive string literal, enclosed within <# #>, that is the same as a literal string, except that it allows nesting of strings.

'a'                                              // character literal
'\n'                                             // also a character literal
"foo\nbar"                                       // string literal
@"x\n"                                           // same as "x\\n"
@"x
 y"                                              // same as "x\n y"
@"""Hi!"""                                       // "" replaces \" to escape a literal quote mark
<#This string type can contain any symbols including "
and new lines. It does not support escape codes
like "\n".#>                                     // same as "This string type can contain any symbols including \"\nand new lines. "
                                                 //       + "It does not\nsupport escape codes\nlike \"\\n\"."
<#Test <# Inner #> end#>                         // same as "Test <# Inner #> end" (i.e. this string type support recursion.

Nim

var c = 'c'
var s = "foobar"
var l = """foobar
and even
more test here"""

var f = r"C:\texts\text.txt" # Raw string

OASYS Assembler

There are two kinds, strings with quotation marks and strings with braces. Both kinds are treated exactly like numeric tokens for all purposes.

Strings with quotation marks can contain repeated quotation marks to represent a quotation mark, a tilde to represent a line break, or a line break to represent a space (in which case any leading spaces on the following line are ignored).

Strings with braces start with { and end with the next } (they don't nest), and all characters (except a right-brace) are treated as-is.

There are no character literals.

Objeck

Objeck string support is similar to Java except that string elements are 1-byte in length. In addition, string literals may terminated using a NULL character or the string's length calculation.

Objective-C

The same as C, with the addition of the new string literal

@"Hello, world!"

which represents a pointer to a statically allocated string object, of type NSString *, similar to string literals in Java. You can use this literal like other object pointers, e.g. call methods on it [@"Hello, world!" uppercaseString].

OCaml

Characters are contained in single quotes:

# 'a';;
- : char = 'a'

Strings are contained in double quotes:

# "Hello world";;
- : string = "Hello world"

Strings may be split across lines and concatenated using the following syntax: (the newline and any blanks at the beginning of the second line is ignored)

# "abc\
    def";;
- : string = "abcdef"

If the above syntax is not used then any newlines and whitespace are included in the string:

# "abc
   def";;
- : string = "abc\n def"

Another syntax to include verbatim text:

# {id|
    Hello World!
  |id} ;;
- : string = "\n  Hello World!\n"

Octave

Strings can be defined in Octave with single or double quotes. In order to maintain compatible with Matlab, it is recommended to use single quotes for defining strings.

  
    s1 = 'abcd'   % simple string
    s2 = 'ab''cd'   % string containing a single quote using an escaped single quote
    s3 = 'ab"cd'   % simple string containing a double quote
    s4 = "ab'cd"   % string containing a single quote
    s5 = "ab""cd"   % string containing a double quote using an escaped double quote
Output:
octave:5>     s1 = 'abcd'   % simple string
s1 = abcd
octave:6>     s2 = 'ab''cd'   % string containing a single quote using an escaped single quote
s2 = ab'cd
octave:7>     s3 = 'ab"cd'   % simple string containing a double quote
s3 = ab"cd
octave:8>     s4 = "ab'cd"   % string containing a single quote
s4 = ab'cd
octave:9>     s5 = "ab""cd"   % string containing a double quote using an escaped double quote
s5 = ab"cd

Oforth

Oforth uses single quotes for characters and double quotes for strings.

There is no character type : characters are integers representing unicode value of the character.

'a'
'\''
"abcd"
"ab\ncd"
"ab\" and \" cd"

Oz

declare
Digit0 = &0       %% the character '0'
NewLine = &\n     %% a character with special representation
NewLine = &\012   %% characters can also be specified with octals

%% Strings are lists of characters, but can also be written in double quotes:
[&H &e &l &l &o] = "Hello"

AnAtom = 'Hello'         %% single quotes are used for atoms
Atom2 = hello = 'hello'  %% for atoms starting with a lower case letter, they are optional

%% To build strings out of other values, so-called virtual strings are used:
MyName = "Peter"
MyAge = 8
{System.showInfo MyName # " is " # MyAge # " years old."}

PARI/GP

There are just three escapes:

\e escape
\n newline
\t tab

Any other escaped character simply represents itself; \\ and \" are the most useful. There are no characters or character strings as such, but Vectorsmall("string") is very similar to a character array.

Version 2.4.3 added the functions printf and Strprintf which allow interpolation (typically with %Ps).

Pascal

See Delphi

Perl

Perl makes no distinction between single characters and strings. One can use single or double quotes, but they are different. Double-quotes allows you to interpolate variables and escape sequences, while single-quotes do not.

'c';                      # character
'hello';                  # these two strings are the same
"hello";
'Hi $name. How are you?'; # result: "Hi $name. How are you?"
"Hi $name. How are you?"; # result: "Hi Bob. How are you?"
'\n';                     # 2-character string with a backslash and "n"
"\n";                     # newline character
`ls`;                     # runs a command in the shell and returns the output as a string
q/hello/;                 # same as 'hello', but allows custom delimiters, eg: q(hi) and q!hi!
qq/hello/;                # same as "hello", but allows custom delimiters, eg: qq{$hi} and qq#hi#
qw/one two three/;        # same as ('one', 'two', 'three'); constructs a list of the words
qx/ls/;                   # quoted execution, same as `ls`
qr/regex/;                # creates a regular expression
<<END;                    # Here-Document
Hi, whatever goes here gets put into the string,
including newlines and $variables,
until the label we put above
END
<<'END';                  # Here-Document like single-quoted
Same as above, but no interpolation of $variables.
END

Phix

Library: Phix/basics

single character literals (incidentally entirely equivalient to their ascii value) require single quotes, eg

constant UPPERCASEJ = 'J'   -- equivalent to 74

string literals use double quotes, eg

constant hw = "Hello World!",
         mt = ""    -- empty string

Note that 'z' and "z" are quite different. In Phix there is a strong difference between a character and a string.

All strings are ansi or utf8, depending on the encoding of the source file, eg

s = "日本語"

Utf8 strings are byte-subscripted rather than character-subscripted, so s[3] is not necessarily the third character.
Phix strings have a length field in the (internal) header, /and/ a terminating null, so they can be used directly when interfacing to C-style languages.
Phix strings can also be used to hold "raw binary", ie instead of a sequence of characters, a sequence of any bytes in the range 0 to 255.
Strings are fully mutable: you can append, prepend, replace, substitute, and crop characters and slices (/substrings) any way you like, eg

string s = "food"
    s[2..3] = 'e'       -- s is now "feed" (replace all)
    s[2..2] = "east"    -- s is now "feasted" (replace substring)
    s[2..5] = ""        -- s is now "fed"

Special characters may be entered (between quotes) using a back-slash:

       Code     Value   Meaning
        \n       #10     newline
        \r       #13     carriage return
        \b       #08     backspace
        \t       #09     tab
        \\       #5C     backslash
        \"       #22	 double quote (the \ is optional in ', mandatory in ")
        \'       #27	 single quote (the \ is optional in ", mandatory in ')
        \0       #00     null
        \#HH     #HH     any hexadecimal byte
        \xHH     #HH     any hexadecimal byte
        \uH4      -      any 16-bit unicode point, eg "\u1234", max #FFFF
        \UH8      -      any 32-bit unicode point, eg "\U00105678", max #10FFFF

There are no other automatic substitutions or interpolation, other than through explict function calls such as [s]printf().

Strings can also be entered by using triple quotes or backticks intead of double quotes to include linebreaks and avoid any backslash interpretation. If the literal begins with a newline, it is discarded and any immediately following leading underscores specify a (maximum) trimming that should be applied to all subsequent lines. Examples:

ts = ""`
this
string\thing`""

ts = ""`
_____this
     string\thing`""

ts = `this
string\thing`

ts = "this\nstring\\thing"

which are all equivalent.

On a practical note, as long as you have at least 2GB of physical memory, you should experience no problems whatsoever constructing a string with 400 million characters, and you could more than triple that by allocating things up front, however deliberately hogging the biggest block of memory the system will allow is generally considered bad programming practice, and may lead to disk thrashing.

Hex string literals are also supported (mainly for compatibility with OpenEuphoria, x/u/U for 1/2/4 byte codes), eg:

?x"68 65 6c 6c 6f";     -- displays "hello"

PHP

PHP makes no distinction between single characters and strings. One can use single or double quotes, but they are different. Double-quotes allows you to interpolate variables and escape sequences, while single-quotes do not.

'c';                      # character
'hello';                  # these two strings are the same
"hello";
'Hi $name. How are you?'; # result: "Hi $name. How are you?"
"Hi $name. How are you?"; # result: "Hi Bob. How are you?"
'\n';                     # 2-character string with a backslash and "n"
"\n";                     # newline character
`ls`;                     # runs a command in the shell and returns the output as a string
<<END                     # Here-Document
Hi, whatever goes here gets put into the string,
including newlines and $variables,
until the label we put above
END;
<<'END'                   # Here-Document like single-quoted
Same as above, but no interpolation of $variables.
END;

Picat

A string is a list of characters. The string literal is in double quotes (a character is in single quote, e.g 's'):

"string"

It can also be constructed as a list of characters:

['s','t','r','i','n','g']

or as a list of (character) atoms (without single quotes):

[s,t,r,i,n,g]

However, upper case characters must be quoted (otherwise they are considered variables):

['S',t,r,i,n,g]


Quoting of certain characters are with an escape character (\c):

"a string\'s quotes: \"a string\'s quotes\". Spaces: \t\n\l\r"

A string can be written on several lines where the newlines are kept:

  X = "string with
  newlines and
spaces",
  % ...

Using a single \ as the last character on a line makes the line continue without newline.

  X = "string with \
newlines \
and \
spaces",
  % ...

is the same as

string with newlines and spaces


PicoLisp

PicoLisp doesn't have a string data type. Instead, symbols are used. Certain uninterned symbols, called "transient symbols", however, look and behave like strings on other languages.

Syntactically, transient symbols (called "strings" in the following) are surrounded by double quotes.

: "ab\"cd"
-> "ab\"cd"

Double quotes in strings are escaped with a backslash.

ASCII control characters can be written using the hat ('^') character:

: "ab^Icd^Jef"  # Tab, linefeed

There is no special character type or representation. Individual characters are handled as single-character strings:

: (chop "abc")
-> ("a" "b" "c")

: (pack (reverse @))
-> "cba"

A limited handling of here-strings is available with the 'here' function.

Pike

'c';          // Character code (ASCII) (result: 99)
"c";          // String (result: "c")
"\n";         // String (result: newline character)
"hi " + world // String (result: "hi " and the contents of the variable world)
#"multiple line
string using the
preprocessor" // single literal string with newlines in it

PL/I

'H'                  /* a single character as a literal.             */
'this is a string'
''                   /* an empty string literal.                     */
'John''s cat'        /* a literal containing an embedded apostrophe. */
                     /* stored are <<John's cat>>                    */
'101100'b            /* a bit string, stored as one bit per digit.   */

Plain English

A string literal is surrounded by double quotes. Plain English does not make a distinction between string and character literals.

To escape a double quote inside a string literal, use two double quotes.

"a ""string"" literal"

Plain TeX

`a' is for ``apple"
\end

The same as LaTeX case, even though one should say the opposite.

The `` and '' in TeX (plainTeX, LaTeX and many more) are just examples of ligatures.

Pop11

In Pop11 charaters literals are written in inverted quotes (backticks)

`a`  ;;; charater a

String are written in quotes

'a'    ;;; string consisting of single character

Backslash is used to insert special charaters into strings:

'\'\n' ;;; string consisting of quote and newline

PowerShell

PowerShell makes no distinction between characters and strings. Single quoted strings do not interpolate variable contents but double quoted strings do. Also, escape sequences are quoted literally as separate characters within single quotes.

PowerShell here-strings begin with @' (or @") followed immediately by a line break and end with a line break followed by '@ (or "@). Escape sequences and variables are interpolated in @" quotes but not in @' quotes.

Prolog

Standard Prolog has no string types. It has atoms which can be formed in two ways, one of which is wrapping arbitrary text in single quotation marks:

'This is an "atom" and not a string.'

Such atoms can be (and are) treated as immutable strings in Prolog in many cases. Another string-like form wraps text in double quotation marks:

"This 'string' will fool you if you're in a standard Prolog environment."

While this appears as a string to non-Prolog users, it is in reality a linked list of integers with each node containing the integer value of the character (or for Unicode-capable systems, code point) at that location. For example:

?- [97, 98, 99] = "abc".
true.

Individual character constants are special forms of integer (syntax sugar) using a 0' prefix:

?- 97 = 0'a.
true.
Works with: SWI Prolog version 7.0

SWI-Prolog, beginning with version 7, introduced a new native string type. Unless options are specifically set by the user, character sequences wrapped in double quotes are now a string data type. The older list-based version uses back quotes instead:

?- [97, 98, 99] = "abc".
false.
?- [97, 98, 99] = `abc`.
true.

Also starting with SWI-Prolog version 7, quasiquotation became possible. While not exactly a string type directly, they can be (ab)used to give multi-line strings. More importantly, however, they permit special string handling to be embedded into Prolog code, in effect permitting entire other languages inside of Prolog to be used natively as per this example:

test_qq_odbc :-
        myodbc_connect_db(Conn),
        odbc_query(Conn, {|odbc||
select
 P.image,D.description,D.meta_keywords,C.image,G.description
from
 product P, product_description D, category C, category_description G, product_to_category J
where
 P.product_id=D.product_id and
 P.product_id=J.product_id and C.category_id=J.category_id and
 C.category_id=G.category_id
        |}, Row),
        writeln(Row).

In this example, the test_qq_odbc/0 predicate connects to an ODBC database and performs a query. The query is wrapped into a multi-line quasiquotation (beginning with {| and ending with |}) that checks the syntax and security of the query, so not only is the query a multi-line string, it is a **checked** multiline string in this case.

PureBasic

PureBasic supports char in ASCII and UNICODE as well as both dynamic and fixed length strings.

; Characters (*.c), can be ASCII or UNICODE depending on compiler setting
Define.c  AChar='A'
; defines as *.a it will be ASCII and *.u is always UNICODE
Define.a  A='b'
Define.u  U='水'

; Strings is defined as **.s or ending with '$'
Define.s  AStrion     ="String #1"
Define    BStrion.s   ="String #2"
Define    CString$    ="String #3"
; Fixed length stings can be defined if needed
Define XString.s{100} ="I am 100 char long!"

; '"' can be included via CHR() or its predefined constant
Define AStringQuotes$=Chr(34)+"Buu"+Chr(34)+" said the ghost!"
Define BStringQuotes$=#DOUBLEQUOTE$+"Buu"+#DOUBLEQUOTE$+" said yet a ghost!"

To dynamically detect the current sizes of a character, e.g. ASCI or UNICODE mode, StringByteLength() can be used.

Select StringByteLength("X")
  Case 1
    Print("ASCII-mode;  Soo, Hello world!")
  Case 2
    Print("UNICODE-mode; Soo, 您好世界!")
EndSelect

Python

Python makes no distinction between single characters and strings. One can use single or double quotes.

'c' == "c" # character
'text' == "text"
' " '
" ' "
'\x20' == ' '
u'unicode string'
u'\u05d0' # unicode literal

As shown in the last examples, Unicode strings are single or double quoted with a "u" or "U" prepended thereto.

Verbatim (a.k.a. "raw") strings are contained within either single or double quotes, but have an "r" or "R" prepended to indicate that backslash characters should NOT be treated as "escape sequences." This is useful when defining regular expressions as it avoids the need to use sequences like \\\\ (a sequence of four backslashes) in order to get one literal backslash into a regular expression string.

r'\x20' == '\\x20'

The Unicode and raw string modifiers can be combined to prefix a raw Unicode string. This must be done as "ur" or "UR" (not with the letters reversed as it: "ru").

Here-strings are denoted with triple quotes.

''' single triple quote '''
""" double triple quote """

The "u" and "r" prefixes can also be used with triple quoted strings.

Triple quoted strings can contain any mixture of double and single quotes as well as embedded newlines, etc. They are terminated by unescaped triple quotes of the same type that initiated the expression. They are generally used for "doc strings" and other multi-line string expressions --- and are useful for "commenting out" blocks of code.

Quackery

A character literal is denoted by the word char. The character is the first non-whitespace character following char.

A string literal is denoted by the word $. The string is delimited by the first non-whitespace character following $.

Character and string literals illustrated in the Quackery shell (REPL):

/O> char X emit
... char Y emit
... char Z emit cr
... $ "This is a 'string'." echo$ cr
... $ 'This is a "string" too.' echo$ cr
... $ ~This is one with "quotes" and 'apostrophes'.~ echo$ cr
... $ \Any non-whitespace character can be the delimiter.\ echo$ cr
... 
XYZ
This is a 'string'.
This is a "string" too.
This is one with "quotes" and 'apostrophes'.
Any non-whitespace character can be the delimiter.

R

R makes no distinction between characters and strings, and uses single and double quotes interchangeably, though double quotes are considered to be preferred. Verbatim strings are not supported. See ?Quotes for more information.

str1 <- "the quick brown fox, etc."
str2 <- 'the quick brown fox, etc.'
identical(str1, str2)   #returns TRUE

R also supports testing string literals with ==, e.g.,

modestring <- 'row,col'
mode.vec <- unlist(strsplit(modestring, ','))
mode.vec[1] # "row"
mode.vec[2] # "col"
if (mode.vec[2] == 'col') { cat('Col!\n') } # Col! (with no quotes)
if (mode.vec[1] == "row") { cat('Row!\n') } # Row!

R also uses backticks, for creating non-standard variable names (amongst other things).

`a b` <- 4
`a b`   # 4
a b     # Error: unexpected symbol in "a b"

R will print different styles of single and double quote using sQuote and dQuote

options(useFancyQuotes=FALSE)
cat("plain quotes: ", dQuote("double"), "and", sQuote("single"), "\n")

returns

plain quotes:  "double" and 'single'
options(useFancyQuotes=TRUE)
cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")

returns

fancy quotes:  “double” and ‘single’
options(useFancyQuotes="TeX")
cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")

returns

 TeX quotes:  ``double'' and `single'

Racket

Characters are specified as hash-backslash-character, sometime using a name for the character.

  #\a
  #\space
  #\return

Strings are double-quoted, and have most of the usual C-style escapes. To include a double-quote in strings, escape it with a backslash, and the same goes for doubly-escaped backslashes (leading to the usual regexp fun).

Racket source code is read as UTF-8 text so strings can include Unicode characters -- but the internal representation is UCS-4. This includes "\NNN" for octals and "\xHH" for hex and "\uHHHH" for higher characters. See the docs for a complete specification.

Racket also has here strings, and a more sophisticated facility for text that includes interpolation-like features, which is described in the Here Document entry

Raku

(formerly Perl 6) Unlike most languages that hardwire their quoting mechanisms, the quote mechanism in Raku is extensible, and all normal-looking quotes actually derive from a parent quoting language called Q via grammatical mixins, applied via standard Raku adverbial syntax. The available quote mixins, straight from current spec S02, are:

Short       Long            Meaning
=====       ====            =======
:x          :exec           Execute as command and return results
:w          :words          Split result on words (no quote protection)
:ww         :quotewords     Split result on words (with quote protection)
:v          :val            Evaluate word or words for value literals
:q          :single         Interpolate \\, \q and \' (or whatever)
:qq         :double         Interpolate with :s, :a, :h, :f, :c, :b
:s          :scalar         Interpolate $ vars
:a          :array          Interpolate @ vars
:h          :hash           Interpolate % vars
:f          :function       Interpolate & calls
:c          :closure        Interpolate {...} expressions
:b          :backslash      Interpolate \n, \t, etc. (implies :q at least)
:to         :heredoc        Parse result as heredoc terminator
            :regex          Parse as regex
            :subst          Parse as substitution
            :trans          Parse as transliteration
            :code           Quasiquoting
:p          :path           Return a Path object (see S16 for more options

In any case, an initial Q, q, or qq may omit the initial colon to form traditional Perl quotes such as qw//. And Q can be used by itself to introduce a quote that has no escapes at all except for the closing delimiter:

my $raw = Q'$@\@#)&!#';

Note that the single quotes there imply no single quoting semantics as they would in Perl 5. They're just the quotes the programmer happened to choose, since they were most like the raw quoting. Single quotes imply :q only when used as normal single quotes are, as discussed below. As in Perl 5, you can use any non-alphanumeric, non-whitespace characters for delimiters with the general forms of quoting, including matching bracket characters, including any Unicode brackets.

Using the definitions above, we can derive the various standard "sugar" quotes from Q, including:

Normal  Means
======  =====
q/.../  Q :q /.../
qq/.../ Q :qq /.../
'...'   Q :q /.../
"..."   Q :qq /.../
<...>   Q :q :w :v /.../
«...»   Q :qq :ww :v /.../
/.../   Q :regex /.../
quasi {...} Q :code {...}

The :qq-derived languages all give normal Perlish interpolation, but individual interpolations may be chosen or suppressed with extra adverbs.

Unlike in Perl 5, we don't use backticks as shorthand for what is now expressed as qqx// in Raku. (Backticks are now reserved for user-defined syntax.) Heredocs now have no special << syntax, but fall out of the :to adverb:

say qq:to/END/;
    Your ad here.
    END

Indentation equivalent to the ending tag is automatically removed.

Backslash sequences recognized by :b (and hence :qq) include:

"\a"        # BELL
"\b"        # BACKSPACE
"\t"        # TAB
"\n"        # LINE FEED
"\f"        # FORM FEED
"\r"        # CARRIAGE RETURN
"\e"        # ESCAPE
"\x263a"    # ☺
"\o40"      # SPACE
"\0"        # NULL

"\cC"       # CTRL-C
"\c8"       # BACKSPACE
"\c[13,10]" # CRLF
"\c[LATIN CAPITAL LETTER A, COMBINING RING ABOVE]"

Leading 0 specifically does not mean octal in Perl 6; you must use \o instead.

Retro

Strings begin with a single quote and end on space. Underscores are replaced with spaces.

ASCII characters are prefixed by a single dollar sign.

$c
'hello,_world!
'This_is_'a_string'

REXX

There are two types of quotes used for REXX literals:

  •   "     (sometimes called a double quote or quote)
  •   '     (sometimes called a single quote or apostrophe)

There is no difference between them as far as specifying a REXX literal.
You can double them (code two of them adjacent) to specify a quote within the string.

char1 = "A"
char2 = 'A'
str = "this is a string"
another = 'this is also a string'
escape1 = "that's it!"
escape2 = 'that''s it!'

Variable expansion is not possible within REXX literals.
Simply concatenate the string with the variable:

amount = 100
result = "You got" amount "points."
say result
Output:
You got 100 points.

It's also possible to express characters in hexadecimal notation in a string:

lf = '0A'x
cr = '0D'x

mmm = '01 02 03 34 ee'x
ppp = 'dead beaf 11112222 33334444 55556666 77778888 00009999 c0ffee'X

lang = '52455858'x     /*which is "REXX" on ASCII-computers.*/

Binary strings are also possible:

jjj = '01011011'B
jjj = '01011011'b
jjj = "0101 1011"b
jjj = '0101 1011 1111'b
longjjj = '11110000 10100001 10110010 11100011 11100100'B

Ring

see 'This is a "quoted string"'

Ruby

Quotes that do not interpolate:

'single quotes with \'embedded quote\' and \\backslash'
%q(not interpolating with (nested) parentheses
and newline)

Quotes that interpolate:

a = 42
"double quotes with \"embedded quote\"\nnewline and variable interpolation: #{a} % 10 = #{a % 10}"
%Q(same as above)
%|same as above|

Heredocs

print <<HERE
With an unquoted delimiter, this interpolates:
a = #{a}
HERE
print <<-INDENTED
   This delimiter can have whitespace before it
   INDENTED
print <<'NON_INTERPOLATING'
This will not interpolate: #{a}
NON_INTERPOLATING

Rust

A char in Rust is a Unicode scalar value. A char type in Rust is always four bytes in size and can be denoted by single quotes:

let char01: char = 'a';
let char02: char = '\u{25A0}'; // Black square
let char03: char = '❤'; // Heart

Rust has two common string types: &str and String. These different string types are used depending if it's a fixed string literal that is saved into the executable and lives throughout the program execution (usually borrowed as a &str), a string slice that references a contiguous sequence of elements (borrowed as a &str), or a String which allows for a growable heap allocated valid UTF-8 string. Only the String type is fully owned by its variable, and the other two are only interacted through the reference &str. The &str string slice type may point to a string literal or a heap-allocated string.

The String type in Rust is intended to always contain a valid UTF-8 string. UTF-8 is a "variable width" encoding, and therefore Strings are going to be typically smaller than an array of the same Rust char's. For example, the String "hi" is all within ASCII, and therefore a UTF-8 String of "hi" represents each character as one byte each. On the other hand, a char array ['h', 'i'] would take up 8 bytes in total. A string is denoted by double quotes:

const string_literal_str1: &str = "Hi Rust!";

let string_slice_str1: &str = string_literal_str1; // Creating a string slice from a string literal
let string_slice_str2: &str = "hello str"; // String slice pointing to string literal "hello str"

let string1: String = String::new(); // Empty String
let string2: String = String::from("hello"); // Creating String from string literal "hello"
let string3: String = "hi".to_string();
let string4: String = "bye".to_owned();
let string5: String = "see you soon".into();
// The "to_string()", "to_owned" or "into" are all equivalent in the code above. 
// The "to_string()", "to_owned" or "into" methods are needed so that a string slice (&str) or a string literal (&str) is explicitly converted into a heap-allocated fully-owned String type. Otherwise the compiler's type checker will complain "expected struct `String`, found `&str` (string slice)"

let string6: String = string_slice_str2.to_owned(); // Explictly converting the string_slice_str2 into a heap-allocated fully-owned String. This can be done with "to_string()", "to_owned" or "into".

// String slices can also point to heap allocated strings:
let string_slice_str3: &str = &string2; // Creating a string slice to a heap-allocated String.
let string7: String = string_slice_str3.to_string(); // Converting string_slice_str3 into a heap-allocated fully-owned String copy, resulting in a new independent owned string copy of the original String. This can be done with "to_string()", "to_owned" or "into".

Rust supports verbatim strings and here-strings by putting r# before the first double quotes, and the end is marked by adding a # after the final double quotes. If there is a "# inside the contents of your here-string or your verbatim string then you can just add more #'s as required in both the beggining and the end:

let verbatim_here_string01: &str = r#"A \verbatim string\, line breaks in programming use \n and tabs \t"#;
let verbatim_here_string02: &str = r#"
A \multi-line\ string, in programming
line breaks use the characters \n
and for tabs we use the characters \t
"#;
let verbatim_here_string03: &str = r##"
Part number "#001": 1
Part number "#002": 2
Part number "#003": 3
"##;

To expand variables in Rust we have 3 options: we can use the "format!" macro, the "print!" macro or the "println!" macro.

let number: i32 = 42;
let number_string: String = format!("Number: {}", number);
println!("The result in string form is '{}'.", number_string);
print!("The number is {}. ", number); // Print without line break
println!("Again, it's {}", number); // Print with line break
// The above prints:
// The result in string form is 'Number: 42'.
// The number is 42. Again, it's 42

In Rust, there are other string types that are specialized to specific string requirements, such as working with system strings (OsString and OsStr), working with C strings (CString and CStr), and working with system paths (Path and PathBuf).

S-lang

S-Lang supports character literals with single-quote apostrophe. These are normally limited to byte sized 0 thru 255 (ASCII, extended into 8 bits). Wide (unicode) character literals can be introduced with apostrophe '\x{Hex}', for example '\x{1D7BC}' for math symbol sans-serif bold italic small sigma. Character literals are treated as Integer_Type data.

Double-quotes are used for strings, but these may not include literal newlines or NUL bytes without using backslash escapes. S-Lang 2.2 introduced verbatim strings using backtick. These may include newline literals, but other platform concerns can make NUL byte literals in the source code a tricky business. Both double-quote and back-quote allow a suffix:

  • R for no backslash escapes (default for back-quoted strings)
  • Q to force backslash escaping (default for double-quoted strings)
  • B to produce a binary string, BString_Type
  • $ to request dollar prefix variable substitutions within the given string.
% String literals
variable c, ch, s, b, r, v;

c = 'A';
ch = '\x{1d7bc}';

printf("Double quotes\n");
s = "this is a single line string with\t\tbackslash substitutions";
() = fputs(s, stdout);
() = fputs("\n", stdout);

s = "this is a single line string without\t\tbackslash substitutions"R;
() = fputs(s, stdout);
() = fputs("\n", stdout);

s = "string with backslash escaped newline \
takes up two lines in source, but no newline is in the string";
() = fputs(s, stdout);
() = fputs("\n", stdout);

printf("\nBack quotes\n");
r = `this is a multi line string with
  backslash substitutions and \t(tabs)\t`Q; 
() = fputs(r, stdout);
() = fputs("\n", stdout);

r = `this is a multi line string without
  backslash substitutions and \t(tabs)\t`;
() = fputs(r, stdout);
() = fputs("\n", stdout);

printf("\nvariable substitution\n");
v = "variable substitution with $$c as $c"$;
() = fputs(v, stdout);
() = fputs("\n", stdout);

v = "no variable substitutions, $$c as $c";
() = fputs(v, stdout);
() = fputs("\n", stdout);

printf("\nBString_Type\n");
b = "this is a binary string, NUL \0 \0 bytes allowed"B;
print(b);
% display of b will be stopped at the NUL byte using stdio streams
() = fputs(b, stdout);
() = fputs("\n", stdout);
printf("strlen(b) is %d, bstrlen(b) is %d\n", strlen(b), bstrlen(b));
Output:
prompt$ slsh strlit.sl
Double quotes
this is a single line string with               backslash substitutions
this is a single line string without\t\tbackslash substitutions
string with backslash escaped newline takes up two lines in source, but no newline is in the string

Back quotes
this is a multi line string with
  backslash substitutions and   (tabs)
this is a multi line string without
  backslash substitutions and \t(tabs)\t

variable substitution
variable substitution with $c as 65
no variable substitutions, $$c as $c

BString_Type
"this is a binary string, NUL \000 \000 bytes allowed"
this is a binary string, NUL
strlen(b) is 29, bstrlen(b) is 46

With substitutions, if a literal dollar sign or backquote is required, the character needs to be doubled. "$$c" is the literal "$c" when using dollar suffixed strings.

Backslash escapes include:

  • \" -- double quote
  • \' -- single quote
  • \\ -- backslash
  • \0 -- NUL byte
  • \a -- bell character (ASCII 7)
  • \t -- tab character (ASCII 9)
  • \n -- newline character (ASCII 10)
  • \e -- escape character (ASCII 27)
  • \xhh -- byte expressed in HEXADECIMAL notation
  • \ooo -- byte expressed in OCTAL notation
  • \dnnn -- byte expressed in DECIMAL
  • \u{h..h} -- the Unicode character U+h..h
  • \x{h..h} -- the Unicode character U+h..h [modal]

Scala

Character literals use single quotes marks:

val c = 'c'

However, symbols are denoted with a single quote, so care must be taken not to confuse the two:

val sym = 'symbol

Strings can use either double quotes, or three successive double quotes. The first allows special characters, the second doesn't:

scala> "newline and slash: \n and \\"
res5: java.lang.String =
newline and slash:
 and \

scala> """newline and slash: \n and \\"""
res6: java.lang.String = newline and slash: \n and \\

However, Unicode characters are expanded wherever they happen, even inside comments. So, for instance:

scala> val uniquote = \u0022normal string"
uniquote: java.lang.String = normal string

scala> val insidequote = """an inside \u0022 quote"""
insidequote: java.lang.String = an inside " quote

Finally, on version 2.7, the triple-double-quoted string ends at the third consecutive quote, on version 2.8 it ends on the last quote of a series of at least three double-quotes.

Scala 2.7

scala> val error = """can't finish with a quote: """"
<console>:1: error: unterminated string
       val error = """can't finish with a quote: """"
                                                    ^

Scala 2.8

scala> val success = """but it can on 2.8: """"
success: java.lang.String = but it can on 2.8: "

Scheme

Characters are specified using the "#\" syntax:

#\a
#\A
#\?
#\space
#\newline

Strings are contained in double quotes:

"Hello world"

Literal symbols, lists, pairs, etc. can be quoted using the quote syntax:

'apple
'(1 2 3) ; same as (list 1 2 3)
'()      ; empty list
'(a . b) ; same as (cons 'a 'b)

Seed7

The type char describes Unicode characters encoded with UTF-32. A character literal is written as UTF-8 encoded Unicode character enclosed in single quotes.

var char: ch is 'z';

The type string describes sequences of Unicode characters. The characters in the string use the UTF-32 encoding. A string literal is a sequence of UTF-8 encoded Unicode characters surrounded by double quotes.

var string: stri is "hello";

This means that 'z' and "z" are different. The former is a character while the latter is a string. Seed7 strings are not null terminated (they do not end with \0). They can contain any sequence of UNICODE (UTF-32) characters (including a \0). Empty strings are also allowed. In order to represent non-printable characters and certain printable characters the following escape sequences may be used.

audible alert BEL \a
backspace BS \b
escape ESC \e
formfeed FF \f
newline NL (LF) \n
carriage return CR \r
horizontal tab HT \t
vertical tab VT \v
backslash (\) \\
apostrophe (') \'
double quote (") \"
control-A \A
...
control-Z \Z

A backslash followed by an integer literal and a semicolon is interpreted as character with the specified ordinal number. Note that the integer literal is interpreted decimal unless it is written as based integer.

"Euro sign: \8364;"

There is also a possibility to break a string into several lines.

var string: example is "this is a string\
                       \ which continues in the next line\n\
                       \and contains a line break";

There is no built-in mechanism for expanding variables within strings.

Sidef

Quotes that do not interpolate:

'single quotes with \'embedded quote\' and \\backslash';
‚unicode single quoted’;
%q(not interpolating with (nested) parentheses
and newline);

Quotes that interpolate:

var a = 42;
"double \Uquotes\E with \"embedded quote\"\nnewline and variable interpolation: #{a} % 10 = #{a % 10}";
„same as above”;
%Q(same as above);

Heredocs:

print <<EOT
Implicit double-quoted (interpolates):
a = #{a}
EOT

print <<"EOD"
Explicit double-quoted with interpolation:
a = #{a}
EOD

print <<'NON_INTERPOLATING'
This will not interpolate: #{a}
NON_INTERPOLATING

Slate

Characters are specified using the $ syntax:

$a
$D
$8
$,
$\s
$\n

Strings are contained in single quotes, with backslash for escaping:

'Hello\'s the word.'

SQL

String literals in SQL use single-quotation. There are no escapes, but you can double a ' mark to make a single ' in the text.

SELECT 'The boy said ''hello''.';

Standard ML

Characters are contained in the #"" syntax:

- #"a";
val it = #"a" : char

Strings are contained in double quotes:

- "Hello world";
val it = "Hello world" : string

Strings may be split across lines and concatenated by having two backslashes around the newline and whitespace:

- "abc\
    \def";
val it = "abcdef" : string

Swift

let you = "You"
let str1 = "\(you) can insert variables into strings."
let str2 = "Swift also supports unicode in strings ı∫ƒ∂ß´™¡à"
let str3 = "Swift also supports control characters \n\tLike this"
let str4 = "'" // '
let str5 = "\"" // "
println(str3)
Output:
Swift also supports control characters 
	Like this


Swift 4 introduced multi-line string literals called long strings. Long strings are strings delimited by """triple quotes""" that can contain newlines and individual " characters without the need to escape them.

let author = "Author"
let xml == """
    <?xml version="1.0"?>
    <catalog>
        <book id="bk101" empty="">
            <author>\(author)</author>
            <title>XML Developer's Guide</title>
            <genre>Computer</genre>
            <price>44.95</price>
            <publish_date>2000-10-01</publish_date>
            <description>An in-depth look at creating applications with XML.</description>
        </book>
    </catalog>
    """

println(xml)
Output:
<?xml version="1.0"?>
<catalog>
    <book id="bk101" empty="">
        <author>Author</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>
        <description>An in-depth look at creating applications with XML.</description>
    </book>
</catalog>

To allow free formatting of the literal an indentation stripping operation is applied whereby any whitespace characters in front of the closing delimiter are removed from each of the lines in the literal. As part of this process any initial linefeed is also removed. This allows the developer to paste literal content directly into the string without modification.

Tcl

Tcl makes no distinction between single characters and strings.

Double quotes allow command and variable interpolation:

set str "This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]"
puts $str ;# ==> This is Tcl 8.5	It is Mon Apr 06 16:49:46 EDT 2009

Braces prevent interpolation

set str {This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]}
puts $str ;# ==> This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]

TI-89 BASIC

Double quotes enclose strings, e.g. "Hello Rosetta Code". There are no escape characters. Quotes in strings are doubled: "This > "" < is one double-quote."

TOML

There are four ways to express strings: basic, multi-line basic, literal, and multi-line literal. All strings must contain only valid UTF-8 characters.

Basic strings are surrounded by quotation marks. Any Unicode character may be used except those that must be escaped: quotation mark, backslash, and the control characters other than tab (U+0000 to U+0008, U+000A to U+001F, U+007F).

str = "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."

Multi-line basic strings are surrounded by three quotation marks on each side and allow newlines. A newline immediately following the opening delimiter will be trimmed. All other whitespace and newline characters remain intact.

str1 = """
Roses are red
Violets are blue"""

When the last non-whitespace character on a line is a \ ("line ending backslash"), it will be trimmed along with all whitespace (including newlines) up to the next non-whitespace character or closing delimiter. All of the escape sequences that are valid for basic strings are also valid for multi-line basic strings.

# The following strings are byte-for-byte equivalent:
str1 = "The quick brown fox jumps over the lazy dog."

str2 = """
The quick brown \


  fox jumps over \
    the lazy dog."""

str3 = """\
       The quick brown \
       fox jumps over \
       the lazy dog.\
       """

Literal strings are surrounded by single quotes and do not support escaping. This means that there is no way to write a single quote in a literal string. Like basic strings, they must appear on a single line.

# What you see is what you get.
winpath  = 'C:\Users\nodejs\templates'
winpath2 = '\\ServerX\admin$\system32\'
quoted   = 'Tom "Dubs" Preston-Werner'
regex    = '<\i\c*\s*>'

Multi-line literal strings are surrounded by three single quotes on each side and allow newlines. Like literal strings, there is no escaping whatsoever. A newline immediately following the opening delimiter will be trimmed. All other content between the delimiters is interpreted as-is without modification. One or two single quotes are allowed anywhere within a multi-line literal string, but sequences of three or more single quotes are not permitted.

regex2 = '''I [dw]on't need \d{2} apples'''
lines  = '''
The first newline is
trimmed in raw strings.
   All other whitespace
   is preserved.
'''

TUSCRIPT

$$ MODE TUSCRIPT,{}
s1=*
DATA "string"
s2=*
DATA + "double" quotes
s3=*
DATA + 'single' quotes
s4=*
DATA + "double" + 'single' quotes
show=JOIN(s1," ",s2,s3,s4)
show=JOIN(show)
PRINT show
Output:
"string" + "double" quotes + 'single' quotes + "double" + 'single' quotes

UNIX Shell

Works with: Bourne Shell
Works with: bash

The Unix shell supports several types of quotation marks:

  • singlequotes - for literal string quotation
  • doublequotes - for interpolated string quotation
  • backticks - Used to capture the output from an external program

Quotation marks within a literal String

It is possible to place singlequote characters within a string enclosed with doublequotes and to put doublequote characters in a string enclosed within singlequotes:

echo "The boy said 'hello'."
echo 'The girl said "hello" too.'

We can also use an escapesequence to put doublequote characters in an interpolated string:

print "The man said \"hello\".";

Here documents

The shell supports the use of here documents for the passing of quoted text as input into a command. Here documents cannot be used to represent literal strings as an expression for variable assignment.

cat << END
1, High Street,
SMALLTOWN,
West Midlands.
WM4 5HD.
END

Ursala

Single characters are denoted with a back quote.

a = `x

Unprintable character constants can be expressed like this.

cr = 13%cOi&

Strings are enclosed in single forward quotes.

b = 'a string'

A single quote in a string is escaped by another single quote.

c = 'Hobson''s choice'

Multi-line strings are enclosed in dash-brackets.

d = 

-[this is a list
of strings]-

Dash-bracket enclosed text can have arbitrary nested unquoted expressions, provided they evaluate to lists of character strings.

e = -[the front matter -[ d ]- the rest of it]-

f = -[text -[ d ]- more -[ e ]- text ]-

This notation can also be used for defining functions.

g "x" = -[ Dear -[ "x" ]- bla bla ]-

The double quotes aren't for character strings but dummy variables.

V

A simple quoted string is of the form 'string' e.g

'hello world' puts

Vim Script

A string constant delimited by double quotes " may contain escape sequences like \n, \t, \123 (byte value in octal), \xab (byte value in hexadecimal), \\ (backslash), \" or \u12ff (character code in hexadecimal according to the current encoding).

If a string constant is delimited by single quotes ' all characters are taken as they are. In order to use a single quote inside a literal string it must be escaped with another single quote, i.e. two single quotes stand for one.

Strings must always end at the current line and characters are just strings of length one.

Visual Basic

Works with: Visual Basic version 5
Works with: Visual Basic version 6
Works with: VBA version Access 97
Works with: VBA version 6.5
Works with: VBA version 7.1
  Debug.Print "Tom said, ""The fox ran away."""
  Debug.Print "Tom said, 'The fox ran away.'"
Output:
Tom said, "The fox ran away."
Tom said, 'The fox ran away.'

Visual Basic .NET

Visual Basic only supports single-line strings. The only escape sequence supported is the double double-quote (""), which is translated into a single double-quote.

Dim s = "Tom said, ""The fox ran away."""
Result: Tom said, "The fox ran away."

V (Vlang)

Character literals for Unicode characters, "rune literals", are an alias for u32.

Character literals for UTF-8 characters, "string literals", are an alias for u8.

To denote rune, Unicode characters, ` (backticks) are used :

A rune can be converted to a UTF-8 string by using the .str() method.

rocket := `🚀`

rocket.str() == '🚀' // uses single quotes, not the backtick, after conversion

A string can be converted back to runes by the .runes() method.

hello := 'Hello World'

hello_runes := hello.runes() // [`H`, `e`, `l`, `l`, `o`, ` `, `W`, `o`, `r`, `l`, `d`]

In V, a string is an immutable array of read-only bytes. All Unicode characters are encoded using UTF-8:

mut s := 'hello 🌎'

s[0] = `H` // not allowed as immutable

// convert `string` to `[]u8`

s := 'hello 🌎'

arr := s.bytes()

assert arr.len == 10

// convert `[]u8` to `string`

s2 := arr.bytestr()

assert s2 == s

// indexing gives a byte, u8(66) == `B`

name := 'Bob'

println(name.len == 3) // will print 3

if name[0] == u8(66) {println(name[0].ascii_str())} // will print`B`

String literals are contained in quotes:

str:= "Hello, world!"

WEB

WEB supports single-quoted strings exactly like Pascal strings (duplicate a ' to represent a literal ').

Double-quoted strings are "pool strings"; they are replaced by a numeric literal by the preprocessor, and placed into a string pool file (duplicate a " to represent a literal ").

Pool strings consisting of exactly one character represent numbers 0-255 according to their ASCII character code.

Wren

In Wren a string is an immutable array of bytes. They are usually interpreted as UTF-8 but don't have to be and invalid UTF-8 sequences are permitted. A string can also include the zero byte (\0) which is not interpreted as a string terminator as would be the case in C.

All strings are instances of the built-in String class and there is no separate Character class as such. Characters are simply strings consisting of a single byte or Unicode code point (1 to 4 bytes).

Ordinary string literals must be surrounded in double quotes and support the following escape characters:

Character Meaning
\0 The NUL byte: 0
\" A double quote character
\\ A backslash
\% A percent sign (see below)
\a Alarm beep
\b Backspace
\e ESC character
\f Form feed
\n Newline
\r Carriage return
\t Tab
\v Vertical tab
\xhh A single byte with hex value '0xhh'
\uhhhh A Unicode code point within the basic multilingual plane
\Uhhhhhhhh Any Unicode code point including emojis

Ordinary string literals also allow interpolation. If you have a percent sign (%) followed by a parenthesized expression, the expression is evaluated and can be arbitrarily complex. Consequently, if you need to include a normal % character in a string literal, you have to use the escaped form \%.

From v0.4.0 Wren also supports raw string literals. These are any text surrounded by triple double quotes, """, and are interpreted verbatim i.e. any control codes and/or interpolations are not processed as such. They can include single or double double quotes without problem.

var s = "abc123"
var t = "abc\t123\%"
var u = "\U0001F64A\U0001F680"
var v = "%("abc" * 3)"
var w = """a"bc""def\n%(v)"""

System.print([s, t, u, v, w])
Output:
[abc123, abc	123%, 🙊🚀, abcabcabc, a"bc""def\n%(v)]

Xojo

Xojo only supports single-line strings. The only escape sequence supported is the double double-quote (""), which is translated into a single double-quote.

Dim s As String = "Tom said, ""The fox ran away."""
Result: Tom said, "The fox ran away."

XPL0

The literal value of a character is specified by preceding it with a
caret. For example, this assigns the ASCII value of A (hex 41) to Ch:

Ch:= ^A;

Strings are one-dimensional byte arrays. 
For example, this assigns the address of the string enclosed in quote marks to Str:

Str:= "Hello, world!";

The ASCII code for each character is stored in sequential bytes. 
By default strings are terminated with the most significant bit set 
on the last character. 
The exclamation point would be stored as hex A1. 
Strings can also be terminated with a zero byte (like in the C language). 
If the command:

string 0;

occurs anywhere before a string is set up then it will have an extra zero
byte at the end.

A quote mark can be included in a string by preceding it with a caret.
Carets are also included this way. 
For example:

"^"^^^" is a ^"caret^""

results in:

"^" is a "caret"

Carets can also be used to specify control characters. 
For example, this is escape E (hex 1B C5):

"^[E"

Strings can any length and can span lines, for example:

"Hello,
world!"

A carriage return (hex 0D) and line feed (hex 0A) are in the middle.

Strings are output to various devices (such as the console screen or
printer) with the Text intrinsic.

XSLT

XSLT is based on XML, and so can use either " or ' to delimit strings. Since XML attribute values are defined using double-quotes, one must use single-quotes for string literals within attributes.

<xsl:if test="starts-with(@name, 'Mr.')">Mister</xsl:if>

Double and single quote characters may also be escaped with XML entities: &quot; and &apos; respectively.

Z80 Assembly

Translation of: 6502 Assembly

Strings are enclosed in double quotes.

db "Hello World"

Any typed character in double quotes is assembled as the ASCII equivalent of that character. Therefore the following two data blocks are equivalent:

db "Hello World"
db $48,$65,$6c,$6c,$6f,$20,$57,$6f,$72,$6c,$64

The assembler typically assumes nothing with regard to special characters. A \n will be interpreted literally, for example. How special characters are handled depends on the printing routine of the hardware's BIOS, or one created by the programmer. If your printing routine is able to support a null terminator and ASCII control codes, the following represents "Hello World" with the new line command and null terminator:

db "Hello World",13,10,0

Creating your own printing routine is a bit out of the scope of this task but here's a simple demonstration that supports the \n and null termination:

PrintString:
; HL contains the pointer to the string literal.
ld a,(hl)
or a                      ;compares to zero quicker than "CP 0"
ret z                     ;we're done printing the string, so exit.
cp '\'                    ; a single ascii character is specified in single quotes. This compares A to the backslash's ASCII value.
jr z,HandleSpecialChars   ; if accumulator = '\' then goto "HandleSpecialChars"
call PrintChar            ;unimplemented print routine, depending on the system this is either a BIOS call 
                          ;    or a routine written by the programmer.

inc hl                    ;next character
jr PrintString            ;back to top


HandleSpecialChars:
inc hl                    ;next char
ld a,(hl)                 
cp 'n'
call z,NewLine            ;unimplemented routine, advances text cursor to next line. Only called if accumulator = 'n'.
inc hl                    ;advance past the 'n' to the next char.
jr PrintString            ;jump back to top. Notice that neither the backslash nor the character after it were actually printed.

zkl

Interpreted string: "hoho". Raw string: 0'|hoho| where | is user choosen. \b, \f, \n, \r, \t, \e escapes are supported. Two adjacent strings are treated as one: "foo" 0'~bar~ --> "foobar". No variable expansion.

here-strings:

text:=
0'|foo|
"bar\n";
Output:
"foobar\n"
n:=7; text:=String(
   "foo = ",3,"\n"
   "bar=",n,"\n"
);
Output:
text = "foo = 3\nbar=7"