Literals/String

From Rosetta Code
Revision as of 03:11, 14 November 2007 by rosettacode>Mwn3d (Added Java example.)
Task
Literals/String
You are encouraged to solve this task according to the task description, using any language you may know.

Show how to quote 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.

C

In C, single characters are contained in single quotes.

char ch = 'z';

Strings are contained in double quotes.

char str = "hello";

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

C has no raw string feature and no way of literally quoting a string containing line breaks. C also has no built-in mechanism for expanding variables within strings.

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.

Java

char a = 'a';
String b = "abc";
char doubleQuote = '"';
char singleQuote = '\'';
String singleQuotes = "''";
String doubleQuotes = "\"\"";

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

`a' is for ``apple''

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.

Python

Python makes no distinction between single characters and strings. One can use single or double quotes. The advantage of having both is that one can use single quotes to quote strings containing literal double quotes and vice versa.

Verbatim strings are contained within either single or double quotes, but have an r or R prepended to indicate that escape sequences should be interpreted literally. This is often useful with regular expressions.

Here-strings are denoted with triple quotes.

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.