Literals/String: Difference between revisions
m →{{header|Java}}: Stupid wiki formatting... |
examples |
||
Line 44:
Python makes no distinction between single characters and strings. One can use single or double quotes.
'c' # character
'text' == "text"
' " '
" ' "
'\x20' == ' '
u'unicode string'
u'\u05d0' # unicode literal
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.
r'\x20' == '\\x20'
Here-strings are denoted with triple quotes.
''' single triple quote '''
""" double triple quote """
=={{header|PowerShell}}==
|
Revision as of 23:58, 14 November 2007
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
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'; //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: ""
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.
'c' # character 'text' == "text" ' " ' " ' " '\x20' == ' ' u'unicode string' u'\u05d0' # unicode literal
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.
r'\x20' == '\\x20'
Here-strings are denoted with triple quotes.
single triple quote """ double triple quote """
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.