Literals/String: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 83: Line 83:
putf(bookf, $3p"Page 3"4l5x"Line 4 indented 5"$)</lang>
putf(bookf, $3p"Page 3"4l5x"Line 4 indented 5"$)</lang>
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
<lang algolw>begin
Line 377: Line 378:
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>
<lang c>#define FOO "prefix_"##MY_SYMBOL##"_suffix"</lang>

=={{header|C++}}==

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

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

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


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
Line 399: Line 389:
Line 2.
Line 2.
Line 3.";</lang>
Line 3.";</lang>

=={{header|C++}}==

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

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

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


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 489: Line 490:
lLine := 'some text';
lLine := 'some text';
lMultiLine := 'some text' + #13#10 + 'on two lines';</lang>
lMultiLine := 'some text' + #13#10 + 'on two lines';</lang>

=={{header|Déjà Vu}}==
<lang dejavu>local :s "String literal"
local :s2 "newline \n carriage return \r tab \t"
!print "backslash \\ quote \q decimal character \{8364}"</lang>
{{out}}
<pre>backslash \ quote " decimal character €</pre>


=={{header|DWScript}}==
=={{header|DWScript}}==
Line 514: Line 508:


Multiline strings are not currently supported.
Multiline strings are not currently supported.

=={{header|Déjà Vu}}==
<lang dejavu>local :s "String literal"
local :s2 "newline \n carriage return \r tab \t"
!print "backslash \\ quote \q decimal character \{8364}"</lang>
{{out}}
<pre>backslash \ quote " decimal character €</pre>


=={{header|E}}==
=={{header|E}}==
Line 609: Line 610:
'string hi'<br>
'string hi'<br>
[115, 116, 114, 105, 110, 103, 32, 104, 105, 0]<br>
[115, 116, 114, 105, 110, 103, 32, 104, 105, 0]<br>

=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
===Strings===
===Strings===
Line 850: Line 852:
<lang Fortran> TEXT = "That's"//CHAR(10)//"right!" !For an ASCII linefeed (or newline) character.</lang>
<lang Fortran> TEXT = "That's"//CHAR(10)//"right!" !For an ASCII linefeed (or newline) character.</lang>
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}}==
<lang 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.'"
</lang>


=={{header|friendly interactive shell}}==
=={{header|friendly interactive shell}}==
Line 888: Line 899:
<lang gecho>'yo...dawg. print</lang>
<lang gecho>'yo...dawg. print</lang>
A string.
A string.

=={{header|FreeBASIC}}==
<lang 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.'"
</lang>


=={{header|Go}}==
=={{header|Go}}==
Line 1,359: Line 1,361:
in combination with the trimMargin function.
in combination with the trimMargin function.
</pre>
</pre>

=={{header|LabVIEW}}==
LabVIEW is a graphical language so it uses graphical string delimiters. No escaping is needed.<br/>{{VI snippet}}<br/>[[File:LabVIEW_Literals_String.png]]


=={{header|Lasso}}==
=={{header|Lasso}}==
Line 1,371: Line 1,376:
<lang Lasso>`I'm also a 2" string\n`</lang>
<lang Lasso>`I'm also a 2" string\n`</lang>


=={{header|LabVIEW}}==
LabVIEW is a graphical language so it uses graphical string delimiters. No escaping is needed.<br/>{{VI snippet}}<br/>[[File:LabVIEW_Literals_String.png]]
=={{header|LaTeX}}==
=={{header|LaTeX}}==


Line 1,528: Line 1,531:
/* A character - just a one character string */
/* A character - just a one character string */
"a"</lang>
"a"</lang>

=={{header|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

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

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>


=={{header|ML/I}}==
=={{header|ML/I}}==
Line 1,548: Line 1,561:
<lang ML/I>This is the first mention of Alice
<lang ML/I>This is the first mention of Alice
and here we mention Bob again</lang>
and here we mention Bob again</lang>

=={{header|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

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

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>


=={{header|Modula-3}}==
=={{header|Modula-3}}==
Line 1,628: Line 1,631:


There are no character literals.
There are no character literals.

=={{header|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.


=={{header|Objective-C}}==
=={{header|Objective-C}}==
Line 1,633: Line 1,640:
<lang objc>@"Hello, world!"</lang>
<lang objc>@"Hello, world!"</lang>
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>.

=={{header|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.


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 1,760: Line 1,763:
Same as above, but no interpolation of $variables.
Same as above, but no interpolation of $variables.
END</lang>
END</lang>

=={{header|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.
The available quote mixins, straight from current spec S02, are:
<pre>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</pre>
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:
<lang perl6>my $raw = Q'$@\@#)&!#';</lang>
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.

Using the definitions above, we can derive the various standard "sugar" quotes from Q, including:
<pre>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 {...}</pre>
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.
(Backticks are now reserved for user-defined syntax.)
Heredocs now have no special <tt><<</tt> syntax,
but fall out of the <tt>:to</tt> adverb:
<lang perl6>say qq:to/END/;
Your ad here.
END</lang>
Indentation equivalent to the ending tag is automatically removed.

Backslash sequences recognized by <tt>:b</tt> (and hence <tt>:qq</tt>) include:
<lang perl6>"\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]"</lang>
Leading <tt>0</tt> specifically does not mean octal in Perl 6;
you must use <tt>\o</tt> instead.


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,958: Line 1,891:
The same as [[Quotes#LaTeX|LaTeX case]], even though one should say the opposite.
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.



=={{header|Pop11}}==
=={{header|Pop11}}==
Line 2,160: Line 2,092:
Racket also has here strings, and a more sophisticated facility for text
Racket also has here strings, and a more sophisticated facility for text
that includes interpolation-like features, which is described in the [http://rosettacode.org/wiki/Here_document#Racket Here Document] entry
that includes interpolation-like features, which is described in the [http://rosettacode.org/wiki/Here_document#Racket Here Document] entry

=={{header|Raku}}==
(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.
The available quote mixins, straight from current spec S02, are:
<pre>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</pre>
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:
<lang perl6>my $raw = Q'$@\@#)&!#';</lang>
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.

Using the definitions above, we can derive the various standard "sugar" quotes from Q, including:
<pre>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 {...}</pre>
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.
(Backticks are now reserved for user-defined syntax.)
Heredocs now have no special <tt><<</tt> syntax,
but fall out of the <tt>:to</tt> adverb:
<lang perl6>say qq:to/END/;
Your ad here.
END</lang>
Indentation equivalent to the ending tag is automatically removed.

Backslash sequences recognized by <tt>:b</tt> (and hence <tt>:qq</tt>) include:
<lang perl6>"\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]"</lang>
Leading <tt>0</tt> specifically does not mean octal in Perl 6;
you must use <tt>\o</tt> instead.


=={{header|Retro}}==
=={{header|Retro}}==
Line 2,419: Line 2,422:
\def";
\def";
val it = "abcdef" : string</lang>
val it = "abcdef" : string</lang>

=={{header|Swift}}==
=={{header|Swift}}==
<lang Swift>let you = "You"
<lang Swift>let you = "You"