Editing 1638: Backslashes

Jump to: navigation, search

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.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 65: Line 65:
 
Some programming languages provide alternative matching string literal delimiters to limit situations where escaping of delimiters is needed. Often, one can begin and end a string with either a single quote or a double quote. This allows one to write <code>'This is a "quoted" string'</code> if double quote marks are intended in the string literal or <code>"This is a 'quoted' string"</code> if single quote marks are intended. Both kinds of delimiters can't be used in the same string literal, but if one needs to construct a string containing both kinds of quote marks one can often concatenate two string literals, each of which uses a different delimiter.
 
Some programming languages provide alternative matching string literal delimiters to limit situations where escaping of delimiters is needed. Often, one can begin and end a string with either a single quote or a double quote. This allows one to write <code>'This is a "quoted" string'</code> if double quote marks are intended in the string literal or <code>"This is a 'quoted' string"</code> if single quote marks are intended. Both kinds of delimiters can't be used in the same string literal, but if one needs to construct a string containing both kinds of quote marks one can often concatenate two string literals, each of which uses a different delimiter.
  
βˆ’
Another feature that seems to be popular in modern programming languages is to provide an alternative syntax for string delimiters designed specifically to limit leaning toothpick syndrome. For example, in Python, a string literal starting with <code>r"</code> is a "raw string"  [http://en.wikipedia.org/wiki/String_literal#Raw_strings] in which no escape processing is done, with similar semantics for a string starting with <code>@"</code> in C#. This allows one to write <code>r"C:\Users"</code> <!-- Note: In Python, backslashes can still escape the closing delimiter. r"C:\" is a SyntaxError. --> in Python or <code>@"C:\Users"</code> in C# without the need to escape the backslash. This does <em>not</em> allow one to embed the terminating delimiter in the middle of the string and prevents the use of the backslash to encode the newline character as <code>\n</code>, but comes in handy when writing a string encoding of a regular expression in which the backslash is escaping one or more other punctuation characters or a shorthand character class (e.g., <code>\s</code> for a whitespace character). For example, when looking for an anchor tag in HTML, developers may encode the regular expression as <code>&lt;[Aa]\s[^&gt;]*&gt;</code>. If they express this regular expression as a raw string literal, the code looks like  <code>r"&lt;[Aa]\s[^&gt;]*&gt;"</code> instead of <code>"&lt;[Aa]\\s[^&gt;]*&gt;"</code>. The point here is that "leaning toothpick syndrome" is such a real problem that it has influenced programming language implementations.
+
Another feature that seems to be popular in modern programming languages is to provide an alternative syntax for string delimiters designed specifically to limit leaning toothpick syndrome. For example, in Python, a string literal starting with <code>r"</code> is a "raw string"  [http://en.wikipedia.org/wiki/String_literal#Raw_strings] in which no escape processing is done, with similar semantics for a string starting with <code>@"</code> in C#. This allows one to write <code>r"C:\Users"</code> <!-- Note: In Python, backslashes can still escape the closing delimiter. r"C:\" is a SyntaxError. --> in Python or <code>@"C:\Users"</code> in C# without the need to escape the backslash. This does <em>not</em> allow one to embed the terminating delimiter in the middle of the string and prevents the use of the backslash to encode the newline character as <code>\n</code>, but comes in handy when writing a string encoding of a regular expression in which the backslash is escaping one or more other punctuation characters or a shorthand character class (e.g., <code>\s</code> for a whitespace character). For example, when looking for an anchor tag in HTML, I may encode the regular expression as <code>&lt;[Aa]\s[^&gt;]*&gt;</code>. If I express this regular expression as a raw string literal, my code looks like  <code>r"&lt;[Aa]\s[^&gt;]*&gt;"</code> instead of <code>"&lt;[Aa]\\s[^&gt;]*&gt;"</code>. The point here is that leaning toothpick syndrome is such a real problem that it has influenced programming language implementations.
  
 
==Transcript==
 
==Transcript==

Please note that all contributions to explain xkcd may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see explain xkcd:Copyrights for details). Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel | Editing help (opens in new window)