What character escape sequences are available?

What character escape sequences are available?

Rate This
  • Comments 20

C# defines the following character escape sequences:

  • \' - single quote, needed for character literals
  • \" - double quote, needed for string literals
  • \\ - backslash
  • \0 - Unicode character 0
  • \a - Alert (character 7)
  • \b - Backspace (character 8)
  • \f - Form feed (character 12)
  • \n - New line (character 10)
  • \r - Carriage return (character 13)
  • \t - Horizontal tab (character 9)
  • \v - Vertical quote (character 11)
  • \uxxxx - Unicode escape sequence for character with hex value xxxx
  • \xn[n][n][n] - Unicode escape sequence for character with hex value nnnn (variable length version of \uxxxx)
  • \Uxxxxxxxx - Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates)

Of these, \a, \f, \v, \x and \U are rarely used in my experience.

[Author: Jon Skeet]

Leave a Comment
  • Please add 2 and 3 and type the answer here:
  • Post
  • Just for the anecdotal value:
    \v is very useful when doing Word automation to generate documents programmatically. Pushing a \v into the doc is equivalent to pressing Shift-Enter in Word itself - it does a line break that doesn't end the paragraph, thus not triggering the auto-numbering or auto-bulleting, as well as many other potentially undesirable effects in the sticky morass of unexpected side-effects that is automating Word. :)
  • thank you
  • good
  • C# Character Escape Sequences' - single quote, needed for character literals" - double quote, needed for string literals\ - backslash0 - Unicode character 0a - Alert (character 7)b - Backspace (character f - Form feed (character 12)n - New line (charac

  • PingBack from http://www.keyongtech.com/2771764-path

  • I am decoding xml files. I read in a line, then wish to replace xml encodings with the original characters. So, for example, I have "abc&def" yet when I try to add the & character to my string, it does not appear. I have tried textstring + '&', textstring + "&", and textstring + '\u0026' but the character is not in the string when I write it out later. What is going on here? (I've been working with c# for a few days only).

  • axwfoss:

    I don't know how it is in WinForms/C#, but in Win32/Delphi a number of GUI elements(such as as labels and menus) use & to mark an accelerator. The & is not displayed then, but instead the following char is underlined.

  • \v is called 'vertical tab' not 'vertical quote'

  • This is cool stuff!!!!

  • I'm new to c# and this will come in handy.  THANKS BOB! ;-] .... my windows phone prof. for those who are wondering who Bob is.

  • One thing I'm confused about: the double backslash \\ is the escape sequence for a backslash, making it useful for specifying file paths, but I just encountered a program that used a double slash // in the filename string literal.  It seems to me that this should not work, but it does.  Why?

  • what is the use of escape character \r in programing?

  • what is the use of escape character \r in programing?

Page 1 of 2 (20 items) 12