Literals/String: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Scala}}: Whitespace)
m (Fixed lang tags.)
Line 4: Line 4:


Single character literals require single quotes
Single character literals require single quotes
<lang Ada>
<lang Ada>ch : character := 'a';</lang>
ch : character := 'a';
</lang>
String literals use double quotes
String literals use double quotes
<lang Ada>
<lang Ada>msg : string := "hello world";
msg : string := "hello world";
empty : string := ""; -- an empty string</lang>
empty : string := ""; -- an empty string
</lang>
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.
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.


=={{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.
CHAR charx = "z";
<lang algol68>CHAR charx = "z";</lang>
Strings are contained in double quotes.
Strings are contained in double quotes.
[]CHAR charxyz = "xyz";
<lang algol68>[]CHAR charxyz = "xyz";
STRING stringxyz = "xyz";
STRING stringxyz = "xyz";
FORMAT twonewlines = $ll$, threenewpages=$ppp$, fourbackspaces=$bbbb$;
FORMAT twonewlines = $ll$, threenewpages=$ppp$, fourbackspaces=$bbbb$;</lang>
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
.PR QUOTE .PR
<lang algol68>.PR QUOTE .PR
[]'CHAR' CHARXYZ = "XYZ";
[]'CHAR' CHARXYZ = "XYZ";</lang>
The STRING type is simply a FLEX array of CHAR.
The STRING type is simply a FLEX array of CHAR.
MODE STRING = FLEX[1:0]CHAR;
<lang algol68>MODE STRING = FLEX[1:0]CHAR;</lang>
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.
BYTES bytesabc = bytes pack("abc");
<lang algol68>BYTES bytesabc = bytes pack("abc");</lang>
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:
STRING stringquote = """I'll be back."" - The Terminator";
<lang algol68>STRING stringquote = """I'll be back."" - The Terminator";</lang>
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:
STRING linexyz := "line X;" +
<lang algol68>STRING linexyz := "line X;" +
"line Y;" +
"line Y;" +
"line Z;";
"line Z;";</lang>
ALGOL 68 uses FORMATs for doing more advanced manipulations.
ALGOL 68 uses FORMATs for doing more advanced manipulations.
For example given:
For example given:
FILE linef; STRING line;
<lang algol68>FILE linef; STRING line;
associate(linef, line);
associate(linef, line);</lang>
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.
FORMAT my_symbol = $"SYMBOL"$;
<lang algol68>FORMAT my_symbol = $"SYMBOL"$;
FORMAT foo = $"prefix_"f(my_symbol)"_suffix"$;
FORMAT foo = $"prefix_"f(my_symbol)"_suffix"$;
putf(linef ,foo);
putf(linef ,foo);</lang>
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.
INT pages=100, lines=25, characters=80;
<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"$)
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|AutoHotkey}}==
=={{header|AutoHotkey}}==
unicode
unicode
<lang AutoHotkey>
<lang AutoHotkey>"c" ; character
"c" ; character
"text" ; string
"text" ; string
hereString = ; with interpolation of %variables%
hereString = ; with interpolation of %variables%
Line 70: Line 65:
=={{header|AWK}}==
=={{header|AWK}}==
Characters are just strings of length 1 in awk, denoted by double quotes.
Characters are just strings of length 1 in awk, denoted by double quotes.
c="x"
<lang awk>c="x"
str="hello"
str="hello"</lang>
Concatenation:
Concatenation:
$ awk 'BEGIN{c="x";s="hello";s=s c;print s}'
<lang awk>$ awk 'BEGIN{c="x";s="hello";s=s c;print s}'
hellox
hellox</lang>


=={{header|BASIC}}==
=={{header|BASIC}}==
Line 95: Line 90:
In C, single characters are contained in single quotes.
In C, single characters are contained in single quotes.


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


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


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


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'.
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'.
Line 106: Line 101:


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:
char lines[] = "line 1\n"
<lang c>char lines[] = "line 1\n"
"line 2\n"
"line 2\n"
"line 3\n";
"line 3\n";</lang>


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:
#define FOO "prefix_"##MY_SYMBOL##"_suffix"
<lang c>#define FOO "prefix_"##MY_SYMBOL##"_suffix"</lang>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
Line 123: Line 118:
=={{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
<lang clojure>
[\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
\\ ; the backslash character literal</lang>
</lang>
There are also identifiers for special characters:
There are also identifiers for special characters:
<lang clojure >
<lang lisp>\space
\space
\newline
\newline
\tab
\tab
\formfeed
\formfeed
\return
\return
\backspace
\backspace</lang>
</lang>
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 clojure >
<lang lisp>"hello world\r\n"</lang>
"hello world\r\n"
</lang>


=={{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>
<lang lisp>(let ((colon #\:)
(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)))</lang>
Line 195: Line 183:
E has three sorts of quotes: ''strings'', ''characters'', and ''quasiliterals''.
E has three sorts of quotes: ''strings'', ''characters'', and ''quasiliterals''.


'T' # character
<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
term`the($adjectives*, fox)` # "term" quasiliteral</lang>


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 206: Line 194:
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:


? if ("<abc,def>" =~ `<@a,@b>`) { [a, b] } else { null }
<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"]
# value: ["abc", "def"]</lang>


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


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


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


The [http://www.haskell.org/onlinereport/lexemes.html#sect2.6 Haskell 98 Report section Character and String Literals] has more information.
The [http://www.haskell.org/onlinereport/lexemes.html#sect2.6 Haskell 98 Report section Character and String Literals] has more information.
Line 230: Line 218:
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:


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


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:


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


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:


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


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:


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


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":


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


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:


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


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):


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


...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 275: Line 263:
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.


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


=={{header|J}}==
=={{header|J}}==
Line 283: Line 271:
Examples:
Examples:


'x' NB. Scalar character
<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<nowiki>''</nowiki>t get simpler' NB. Embedded single-quote
'can<nowiki>''</nowiki>t get simpler' NB. Embedded single-quote</lang>


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:


'Here is line 1',LF,'and line two'
<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'

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

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


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:


CR =: 13 { a.
<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.
TAB =: 9 { a.</lang>


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:


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


For multiline literals, you may define an explicit noun, which is terminated by a lone <tt>)</tt> in the very first column:
For multiline literals, you may define an explicit noun, which is terminated by a lone <tt>)</tt> in the very first column:


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

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

To collect your winnings, please send $PAYMENT
To collect your winnings, please send $PAYMENT
to ADDRESS.
to ADDRESS.
)</lang>
)


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


load 'strings'
<lang j>load 'strings'

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

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


load 'printf'
<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.
to programmers of C.</lang>




=={{header|Java}}==
=={{header|Java}}==
char a = 'a'; //prints as: a
<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 = <nowiki>'\'';</nowiki> //prints as: '
char singleQuote = <nowiki>'\'';</nowiki> //prints as: '
String singleQuotes = <nowiki>"''";</nowiki> //prints as: <nowiki>''</nowiki>
String singleQuotes = <nowiki>"''";</nowiki> //prints as: <nowiki>''</nowiki>
String doubleQuotes = "\"\""; //prints as: ""
String doubleQuotes = "\"\""; //prints as: ""</lang>
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:
this is a test
<lang java>this is a test</lang>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 362: Line 350:
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
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


`a' is for ``apple'''''''
<lang latex>`a' is for ``apple'''''''</lang>


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.
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.
Line 368: Line 356:
=={{header|Lisaac}}==
=={{header|Lisaac}}==
Characters:
Characters:
<lang Lisaac>
<lang Lisaac>c1 := 'a';
c1 := 'a';
c2 := '\n'; // newline
c2 := '\n'; // newline
c3 := '\''; // quote
c3 := '\''; // quote
Line 375: Line 362:
c5 := '\10\'; // decimal
c5 := '\10\'; // decimal
c6 := '\0Ah\'; // hexadecimal
c6 := '\0Ah\'; // hexadecimal
c7 := '\10010110b\'; // binary
c7 := '\10010110b\'; // binary</lang>
</lang>
Strings:
Strings:
<lang Lisaac>
<lang Lisaac>s1 := "this is a\nsample"; // newline
s1 := "this is a\nsample"; // newline
s2 := "\""; // double quote
s2 := "\""; // double quote
s3 := "abc\
s3 := "abc\
\xyz"; // "abcxyz", cut the gap
\xyz"; // "abcxyz", cut the gap</lang>
</lang>


=={{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. 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.
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
<lang logo>print "Hello\,\ world
print "|Hello, world|
print "|Hello, world|</lang>


=={{header|M4}}==
=={{header|M4}}==
Line 422: Line 406:
=={{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
@"Hello, world!"
<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>.


Line 428: Line 412:


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


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


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)
# "abc\
<lang ocaml># "abc\
def";;
def";;
- : string = "abcdef"
- : string = "abcdef"</lang>


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:
# "abc
<lang ocaml># "abc
def";;
def";;
- : string = "abc\n def"
- : string = "abc\n def"</lang>


=={{header|Perl}}==
=={{header|Perl}}==
Line 449: Line 433:
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.
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
<lang perl>'c'; # character
'hello'; # these two strings are the same
'hello'; # these two strings are the same
"hello";
"hello";
'Hi $name. How are you?'; # result: "Hi $name. How are you?"
'Hi $name. How are you?'; # result: "Hi $name. How are you?"
"Hi $name. How are you?"; # result: "Hi Bob. How are you?"
"Hi $name. How are you?"; # result: "Hi Bob. How are you?"
'\n'; # 2-character string with a backslash and "n"
'\n'; # 2-character string with a backslash and "n"
"\n"; # newline character
"\n"; # newline character
`ls`; # runs a command in the shell and returns the output as a string
`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!
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#
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
qw/one two three/; # same as ('one', 'two', 'three'); constructs a list of the words
qx/ls/; # quoted execution, same as `ls`
qx/ls/; # quoted execution, same as `ls`
qr/regex/; # creates a regular expression
qr/regex/; # creates a regular expression
<<END; # Here-Document
<<END; # Here-Document
Hi, whatever goes here gets put into the string,
Hi, whatever goes here gets put into the string,
including newlines and $variables,
including newlines and $variables,
until the label we put above
until the label we put above
END
END
<<'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
END</lang>


=={{header|PHP}}==
=={{header|PHP}}==
Line 475: Line 459:
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.
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
<lang php>'c'; # character
'hello'; # these two strings are the same
'hello'; # these two strings are the same
"hello";
"hello";
'Hi $name. How are you?'; # result: "Hi $name. How are you?"
'Hi $name. How are you?'; # result: "Hi $name. How are you?"
"Hi $name. How are you?"; # result: "Hi Bob. How are you?"
"Hi $name. How are you?"; # result: "Hi Bob. How are you?"
'\n'; # 2-character string with a backslash and "n"
'\n'; # 2-character string with a backslash and "n"
"\n"; # newline character
"\n"; # newline character
`ls`; # runs a command in the shell and returns the output as a string
`ls`; # runs a command in the shell and returns the output as a string
<<END # Here-Document
<<END # Here-Document
Hi, whatever goes here gets put into the string,
Hi, whatever goes here gets put into the string,
including newlines and $variables,
including newlines and $variables,
until the label we put above
until the label we put above
END;
END;
<<'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;
END;</lang>


=={{header|plainTeX}}==
=={{header|plainTeX}}==
Line 505: Line 489:
String are written in quotes
String are written in quotes


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


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


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


=={{header|Python}}==
=={{header|Python}}==
Line 533: Line 517:
Here-strings are denoted with triple quotes.
Here-strings are denoted with triple quotes.


<lang python>''' single triple quote '''
<pre>
''' single triple quote '''
""" double triple quote """</lang>
""" double triple quote """
</pre>


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 551: Line 533:
=={{header|R}}==
=={{header|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 [http://stat.ethz.ch/R-manual/R-patched/library/base/html/Quotes.html ?Quotes] for more information.
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 [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."
<lang R>
str1 <- "the quick brown fox, etc."
str2 <- 'the quick brown fox, etc.'
str2 <- 'the quick brown fox, etc.'
identical(str1, str2) #TRUE
identical(str1, str2) #TRUE</lang>
</lang>
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>
<lang R>`a b` <- 4
`a b` <- 4
`a b` # 4
`a b` # 4
a b # Error: unexpected symbol in "a b"
a b # Error: unexpected symbol in "a b"</lang>
</lang>
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>
<lang R>options(useFancyQuotes=FALSE)
cat("plain quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</lang>
options(useFancyQuotes=FALSE)
cat("plain quotes: ", dQuote("double"), "and", sQuote("single"), "\n")
</lang>
plain quotes: "double" and 'single'
plain quotes: "double" and 'single'
<lang R>
<lang R>options(useFancyQuotes=TRUE)
cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</lang>
options(useFancyQuotes=TRUE)
cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")
</lang>
fancy quotes: “double” and ‘single’
fancy quotes: “double” and ‘single’
<lang R>
<lang R>options(useFancyQuotes="TeX")
cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</lang>
options(useFancyQuotes="TeX")
cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")
</lang>
TeX quotes: ``double'' and `single'
TeX quotes: ``double'' and `single'


Line 618: Line 590:


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


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


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


Line 644: Line 615:


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


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


Literal symbols, lists, pairs, etc. can be quoted using the quote syntax:
Literal symbols, lists, pairs, etc. can be quoted using the quote syntax:
'apple
<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)
'(a . b) ; same as (cons 'a 'b)</lang>


=={{header|Seed7}}==
=={{header|Seed7}}==
Line 663: Line 634:
In Seed7, single characters are contained in single quotes.
In Seed7, single characters are contained in single quotes.


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


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


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


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 (UCS-32) characters (including a \0). In the source code the UNICODE characters are written with the UTF-8 coding. Empty strings are also allowed. The backslash is used to allow double quotes and control characters in strings. There is also a possibility to break a string into several lines.
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 (UCS-32) characters (including a \0). In the source code the UNICODE characters are written with the UTF-8 coding. Empty strings are also allowed. The backslash is used to allow double quotes and control characters in strings. There is also a possibility to break a string into several lines.


var string: example is "this is a string\
<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";
\and contains a line break";</lang>


There is no built-in mechanism for expanding variables within strings.
There is no built-in mechanism for expanding variables within strings.
Line 681: Line 652:
Characters are specified using the <tt>$</tt> syntax:
Characters are specified using the <tt>$</tt> syntax:


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


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


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


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


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


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


=={{header|Tcl}}==
=={{header|Tcl}}==
Line 758: Line 725:
A simple quoted string is of the form 'string'
A simple quoted string is of the form 'string'
e.g
e.g
'hello world' puts
<lang v>'hello world' puts</lang>


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
Line 764: Line 731:
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.


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




=={{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.
<xsl:if test="starts-with(@name, 'Mr.')">Mister</xsl:if>
<lang xml><xsl:if test="starts-with(@name, 'Mr.')">Mister</xsl:if></lang>


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.

Revision as of 19:35, 21 November 2009

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

Show literal specification of characters and strings. If supported, show how verbatim strings (quotes where escape sequences are quoted literally) and here-strings work. Also, discuss which quotes expand variables.

Ada

Single character literals require single quotes <lang Ada>ch : character := 'a';</lang> String literals use double quotes <lang Ada>msg : string := "hello world"; empty : string := ""; -- an empty string</lang> 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.

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. <lang algol68>CHAR charx = "z";</lang> Strings are contained in double quotes. <lang algol68>[]CHAR charxyz = "xyz"; STRING stringxyz = "xyz"; FORMAT twonewlines = $ll$, threenewpages=$ppp$, fourbackspaces=$bbbb$;</lang> 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 <lang algol68>.PR QUOTE .PR []'CHAR' CHARXYZ = "XYZ";</lang> The STRING type is simply a FLEX array of CHAR. <lang algol68>MODE STRING = FLEX[1:0]CHAR;</lang> 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> 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> A string can span lines, but cannot contain newlines. String literals are concatenated when compiled: <lang algol68>STRING linexyz := "line X;" +

"line Y;" + 
"line Z;";</lang>

ALGOL 68 uses FORMATs for doing more advanced manipulations. For example given: <lang algol68>FILE linef; STRING line; associate(linef, line);</lang> Instead of using preprocessor macros ALGOL 68 can do FORMAT variable replacement within FORMATs at run time. <lang algol68>FORMAT my_symbol = $"SYMBOL"$; FORMAT foo = $"prefix_"f(my_symbol)"_suffix"$; putf(linef ,foo);</lang> 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. <lang algol68>INT pages=100, lines=25, characters=80; FILE bookf; FLEX[pages]FLEX[lines]FLEX[characters]CHAR book; associate(bookf, book);

  1. following putf inserts the string " Line 4 indented 5" on page 3 #

putf(bookf, $3p"Page 3"4l5x"Line 4 indented 5"$)</lang> Note: ALGOL 68G does not implement newpage and backspace.

AutoHotkey

unicode <lang AutoHotkey>"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 )</lang>

AWK

Characters are just strings of length 1 in awk, denoted by double quotes. <lang awk>c="x" str="hello"</lang> Concatenation: <lang awk>$ awk 'BEGIN{c="x";s="hello";s=s c;print s}' hellox</lang>

BASIC

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.

<lang qbasic>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)</lang>

Output:

"string data c"

C

In C, single characters are contained in single quotes.

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

Strings are contained in double quotes.

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

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: <lang c>char lines[] = "line 1\n"

"line 2\n"
"line 3\n";</lang>

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>

C#

C# uses single quotes for characters and double quotes for strings just as C. C# also supports verbatim strings. These begin with @" and end with ". Verbatim quotes may contain line breaks and so verbatim strings and here-strings overlap.

C++

Quoting is essentially the same in C and C++. However, C++ adds the ability to prefix an L to an opening quote indicate that a character is a wide character or that a string is an array of wide characters.

Clojure

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

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. <lang lisp>(let ((colon #\:)

     (str "http://www.rosettacode.com/"))
  (format t "colon found at position ~d~%" (position colon str)))</lang>

D

Character literals:

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

Regular strings support C-style escape sequences.

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

Literal string (escape sequences are not interpreted):

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

Specified delimiter string:

<lang d>// 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.$";</lang>

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

   So is this.

EOS";</lang>

Token string:

<lang d>// 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{@?};</lang>

Hex string:

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

E

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

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

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:

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

  1. value: ["abc", "def"]

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

  1. value: ["abc", "def"]</lang>

Haskell

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:

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

           \def"

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

             \def"</lang>

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

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:

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

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>

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

<lang idl>a = ' that''s a string print,a

==> that's a string</lang>

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

<lang idl>b = 'hello' a = b+' world print,a

==> hello world</lang>

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

<lang idl>print,'777'x

==> 1911

print,'777'o

==> 511

print,'777'

==> 777</lang>

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

<lang idl>print,"777

==> 511

print,"877

==> 877</lang>

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

<lang idl>a = "0"

==> Syntax error.</lang>

...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.

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

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:

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

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'

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

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

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

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. LF =: 10 { a. CRLF =: CR,LF NB. Or just 10 13 { a. TAB =: 9 { a.</lang>

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' 'Hello, ',NAME,' you may have already won $1,000,000'</lang>

For multiline literals, you may define an explicit noun, which is terminated by a lone ) in the very first column:

<lang j>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. )</lang>

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

<lang j>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</lang>

While C-like interpolation can be effected with another:

<lang j> load 'printf'

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

This should look 99% familiar to programmers of C.</lang>


Java

<lang 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: ""</lang> 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: <lang java>this is a test</lang>

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.

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

<lang latex>`a' is for ``apple''</lang>

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.

Lisaac

Characters: <lang Lisaac>c1 := 'a'; c2 := '\n'; // newline c3 := '\; // quote c4 := '\101o'; // octal c5 := '\10\'; // decimal c6 := '\0Ah\'; // hexadecimal c7 := '\10010110b\'; // binary</lang> Strings: <lang Lisaac>s1 := "this is a\nsample"; // newline s2 := "\""; // double quote s3 := "abc\

     \xyz"; // "abcxyz", cut the gap</lang>

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. <lang logo>print "Hello\,\ world print "|Hello, world|</lang>

M4

The quoting characters are ` and ', but can be changed by the changequote macro: <lang m4>`this is quoted string'</lang> <lang m4>changequote(`[',`]')dnl [this is a quoted string]</lang>

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>

Modula-3

Characters in Modula-3 use single quotes. <lang modula3>VAR char: CHAR := 'a';</lang> Strings in Modula-3 use double quotes. <lang modula3>VAR str: TEXT := "foo";</lang> 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. <lang modula3>VAR str: TEXT := "Foo"; VAR chrarray: ARRAY [1..3] OF CHAR;

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

Objective-C

The same as C, with the addition of the new string literal <lang objc>@"Hello, world!"</lang> 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: <lang ocaml># 'a';; - : char = 'a'</lang>

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

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\

   def";;

- : string = "abcdef"</lang>

If the above syntax is not used then any newlines and whitespace are included in the string: <lang ocaml># "abc

  def";;

- : string = "abc\n def"</lang>

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.

<lang perl>'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</lang>

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.

<lang php>'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;</lang>

Plain TeX

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

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

Backslash is used to insert special charaters into strings:

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

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.

<lang python> single triple quote """ double triple quote """</lang>

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.

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.

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. <lang R>str1 <- "the quick brown fox, etc." str2 <- 'the quick brown fox, etc.' identical(str1, str2) #TRUE</lang> R also uses backticks, for creating non-standard variable names (amongst other things). <lang R>`a b` <- 4 `a b` # 4 a b # Error: unexpected symbol in "a b"</lang> R will print different styles of single and double quote using sQuote and dQuote <lang R>options(useFancyQuotes=FALSE) cat("plain quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</lang>

plain quotes:  "double" and 'single'

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

fancy quotes:  “double” and ‘single’

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

TeX quotes:  ``double and `single'

Ruby

Quotes that do not interpolate: <lang ruby>'single quotes with \'embedded quote\' and \\backslash' %q(not interpolating with (nested) parentheses and newline)</lang> Quotes that interpolate: <lang ruby>a = 42 "double quotes with \"embedded quote\"\nnewline and variable interpolation: #{a} % 10 = #{a % 10}" %Q(same as above) %|same as above|</lang> Heredocs <lang ruby>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</lang>

Scala

Character literals use single quotes marks:

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

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

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

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

<lang scala>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 \\</lang>

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

<lang scala>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</lang>

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 <lang scala>scala> val error = """can't finish with a quote: """" <console>:1: error: unterminated string

      val error = """can't finish with a quote: """"
                                                   ^</lang>

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

Scheme

Characters are specified using the "#\" syntax: <lang scheme>#\a

  1. \A
  2. \?
  3. \space
  4. \newline</lang>

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

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

Seed7

In Seed7, single characters are contained in single quotes.

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

Strings are contained in double quotes.

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

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 (UCS-32) characters (including a \0). In the source code the UNICODE characters are written with the UTF-8 coding. Empty strings are also allowed. The backslash is used to allow double quotes and control characters in strings. There is also a possibility to break a string into several lines.

<lang seed7>var string: example is "this is a string\

                      \ which continues in the next line\n\
                      \and contains a line break";</lang>

There is no built-in mechanism for expanding variables within strings.

Slate

Characters are specified using the $ syntax:

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

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

Standard ML

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

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

Strings may be split across lines and concatenated by having two backslashes around the newline and whitespace: <lang sml>- "abc\

   \def";

val it = "abcdef" : string</lang>

Tcl

Tcl makes no distinction between single characters and strings.

Double quotes allow command and variable interpolation: <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>

Braces prevent interpolation <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>

TI-89 BASIC

This example may be incorrect.
This has been determined by experiment; no reference documentation was found to use.
Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.

Double quotes enclose strings, e.g. "Hello Rosetta Code". There are no escape characters. Quotes in strings are doubled: "This > "" < is one double-quote."

Ursala

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

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

f = -[text -[ d ]- more -[ e ]- text ]-</lang> This notation can also be used for defining functions. <lang Ursala>g "x" = -[ Dear -[ "x" ]- bla bla ]-</lang> The double quotes aren't for character strings but dummy variables.

V

A simple quoted string is of the form 'string' e.g <lang v>'hello world' puts</lang>

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.

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


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. <lang xml><xsl:if test="starts-with(@name, 'Mr.')">Mister</xsl:if></lang>

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