Csharp/CSharp Tutorial/Data Type/Escape sequences

Материал из .Net Framework эксперт
Версия от 12:18, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Character escape sequence: \0

using System;
class MainClass
{
  static void Main(string[] args)
  {
    char MyChar = "\0";
    
    Console.WriteLine(MyChar);
  }
}

Escape Characters

Escape Sequence        Character Name               Unicode Encoding
\"                     Single quote                 0x0027
\"                     Double quote                 0x0022
\\                     Backslash                    0x005C
\0                     Null                         0x0000
\a                     Alert                        0x0007
\b                     Backspace                    0x0008
\f                     Form feed                    0x000C
\n                     newline                      0x000A
\r                     Carriage return              0x000D
\t                     Horizontal tab               0x0009
\v                     Vertical tab                 0x000B
\uxxxx                 Unicode character in hex     \u0029
    
   

using System;
using System.Collections.Generic;
using System.Text;

  class Program
  {
    static void Main(string[] args)
    {
      string strWithTabs = "Model\tColor\tSpeed\tPet Name\a";
      Console.WriteLine(strWithTabs);
      Console.WriteLine("Everyone loves \"Hello World\"\a");
    }
  }

Escape sequences in strings: embed quotes

using System; 
 
class Example {    
  public static void Main() {    
 
    Console.WriteLine("\"Why?\", he asked."); 
  }
}
"Why?", he asked.

Escape sequences in strings: \n (new line)

using System; 
 
class Example {    
  public static void Main() {    
    Console.WriteLine("Line One\nLine Two\nLine Three"); 
  }    
}
Line One
Line Two
Line Three

Escape sequences in strings: \t (tab)

using System; 
 
class Example {    
  public static void Main() {    
    Console.WriteLine("One\tTwo\tThree"); 
    Console.WriteLine("Four\tFive\tSix"); 
  }
}
One     Two     Three
Four    Five    Six

Special characters

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class MainClass
{
    public static void Main()
    {
        Console.WriteLine("Single quote \\": {0} ({1})", "\"", (int)"\"");
        Console.WriteLine("Double quote \\\": {0} ({1})", "\"", (int)"\"");
        Console.WriteLine("Backslash \\\\: {0} ({1})", "\\", (int)"\\");
        Console.WriteLine("Unicode null \\0: {0} ({1})", "\0", (int)"\0");
        Console.WriteLine("Alert \\a: {0} ({1})", "\a", (int)"\a");
        Console.WriteLine("Backspace \\b: {0} ({1})", "\b", (int)"\b");
        Console.WriteLine("Horizontal tab \\t: {0} ({1})", "\t", (int)"\t");
        Console.WriteLine("Newline \\r: {0} ({1})", "\r", (int)"\r");
        Console.WriteLine("Vertical quote \\v: {0} ({1})", "\v", (int)"\v");
        Console.WriteLine("Form feed \\f: {0} ({1})", "\f", (int)"\f");
        Console.WriteLine("Carriage return \\r: {0} ({1})", "\r", (int)"\r");
    }
}
Single quote \": " (39)
Double quote \": " (34)
Backslash \\: \ (92)
Unicode null \0:   (0)
Alert \a:  (7)
Backspace \b: (8)
Horizontal tab \t:       (9)
 (13)ne \r:
Vertical quote \v:  (11)
Form feed \f:  (12)
 (13)age return \r:

The following string is printed verbatim thus, all escape characters are displayed.

using System;
using System.Collections.Generic;
using System.Text;

  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine(@"C:\MyApp\bin\Debug\a");
    }
  }

White space is preserved with verbatim strings

using System;
using System.Collections.Generic;
using System.Text;

  class Program
  {
    static void Main(string[] args)
    {
      // White space is preserved with verbatim strings.
      string myLongString = @"This is a very
                 very 
                      very 
                           long string";
      Console.WriteLine(myLongString);
    }
  }