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!
=== Raw strings === A few languages provide a method of specifying that a literal is to be processed without any language-specific interpretation. This avoids the need for escaping, and yields more legible strings. Raw strings are particularly useful when a common character needs to be escaped, notably in regular expressions (nested as string literals), where backslash <code>\</code> is widely used, and in DOS/Windows [[Path (computing)|paths]], where backslash is used as a path separator. The profusion of backslashes is known as [[leaning toothpick syndrome]], and can be reduced by using raw strings. Compare escaped and raw pathnames in C#: <syntaxhighlight lang="csharp"> "The Windows path is C:\\Foo\\Bar\\Baz\\" @"The Windows path is C:\Foo\Bar\Baz\" </syntaxhighlight> Extreme examples occur when these are combined β [[Uniform Naming Convention]] paths begin with <code>\\</code>, and thus an escaped regular expression matching a UNC name begins with 8 backslashes, <code>"\\\\\\\\"</code>, due to needing to escape the string and the regular expression. Using raw strings reduces this to 4 (escaping in the regular expression), as in C# <code>@"\\\\"</code>. In XML documents, [[CDATA#CDATA sections in XML|CDATA]] sections allows use of characters such as & and < without an XML parser attempting to interpret them as part of the structure of the document itself. This can be useful when including literal text and scripting code, to keep the document [[Well-formed XML document|well formed]]. <syntaxhighlight lang="xml"> <![CDATA[ if (path!=null && depth<2) { add(path); } ]]> </syntaxhighlight>
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)