Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
String literal
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Different kinds of strings == Some languages provide more than one kind of literal, which have different behavior. This is particularly used to indicate [[#Raw strings|raw strings]] (no escaping), or to disable or enable variable interpolation, but has other uses, such as distinguishing character sets. Most often this is done by changing the quoting character or adding a prefix or suffix. This is comparable to prefixes and suffixes to [[integer literal]]s, such as to indicate hexadecimal numbers or long integers. One of the oldest examples is in shell scripts, where single quotes indicate a raw string or "literal string", while double quotes have escape sequences and variable interpolation. For example, in [[Python (programming language)|Python]], raw strings are preceded by an <code>r</code> or <code>R</code> β compare <code>'C:\\Windows'</code> with <code>r'C:\Windows'</code> (though, a Python raw string cannot end in an odd number of backslashes). Python 2 also distinguishes two types of strings: 8-bit ASCII ("bytes") strings (the default), explicitly indicated with a <code>b</code> or <code>B</code> prefix, and Unicode strings, indicated with a <code>u</code> or <code>U</code> prefix.<ref>{{cite web|url=https://docs.python.org/2/reference/lexical_analysis.html#string-literals|title=2. Lexical analysis β Python 2.7.12rc1 documentation|work=python.org|access-date=22 June 2016}}</ref> while in Python 3 strings are Unicode by default and bytes are a separate <code>bytes</code> type that when initialized with quotes must be prefixed with a <code>b</code>. [[C Sharp (programming language)|C#]]'s notation for raw strings is called @-quoting. <syntaxhighlight lang="csharp"> @"C:\Foo\Bar\Baz\" </syntaxhighlight> While this disables escaping, it allows double-up quotes, which allow one to represent quotes within the string: <syntaxhighlight lang="csharp"> @"I said, ""Hello there.""" </syntaxhighlight> [[C++11#New string literals|C++11 allows]] raw strings, unicode strings (UTF-8, UTF-16, and UTF-32), and wide character strings, determined by prefixes. It also adds literals for the existing C++ <code>string</code>, which is generally preferred to the existing C-style strings. In Tcl, brace-delimited strings are literal, while quote-delimited strings have escaping and interpolation. Perl has a wide variety of strings, which are more formally considered operators, and are known as '''quote and quote-like operators'''. These include both a usual syntax (fixed delimiters) and a generic syntax, which allows a choice of delimiters; these include:<ref>{{cite web|url=http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators|title=perlop - perldoc.perl.org|work=perl.org|access-date=22 June 2016}}</ref> <syntaxhighlight lang="perl"> '' "" `` // m// qr// s/// y/// q{} qq{} qx{} qw{} m{} qr{} s{}{} tr{}{} y{}{} </syntaxhighlight> [[REXX]] uses suffix characters to specify characters or strings using their hexadecimal or binary code. E.g., <syntaxhighlight lang="rexx"> '20'x "0010 0000"b "00100000"b </syntaxhighlight> all yield the [[space character]], avoiding the function call <code>X2C(20)</code>.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)