Csharp/CSharp Tutorial/String/String Escape

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Displaying a Single Quote Using an Escape Sequence

class SingleQuote
{
  static void Main()
  {
      System.Console.WriteLine("\"");
  }
}

Escape characters.

using System;
using System.Text;
class StringApp {
    static void Main(string[] args) {
        string s3 = "Hello\tThere\tAgain";
        Console.WriteLine(s3);
        Console.WriteLine("Everyone loves \"Hello World\"");
        Console.WriteLine("C:\\MyApp\\bin\\debug");
        Console.WriteLine("All finished.\n\n\n");
    }
}

Make changes to string

using System;
using System.Text;
class StringApp {
    static void Main(string[] args) {
        System.String strFixed = "This is how I began life";
        Console.WriteLine(strFixed);
        string upperVersion = strFixed.ToUpper();
        Console.WriteLine(strFixed);
        Console.WriteLine("{0}\n\n", upperVersion);
    }
}

String escape: \\

using System;
class MainClass
{
  static void Main(string[] args)
  {
    string MyString = "c:\\Program Files";
    
    Console.WriteLine(MyString);
  }
}
c:\Program Files

String escape: @ (AT sign)

using System;
class MainClass
{
  static void Main(string[] args)
  {
    string MyString = @"C:\MyFiles";
    
    Console.WriteLine(MyString);
  }
}

String Escape: quote

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

String Literal Escape Characters

Character                Meaning in Life
\"                       Inserts a single quote.
\"                       Inserts a double quote.
\\                       Inserts a backslash into a string literal. 
\a                       Triggers a system alert (beep). 
\n                       Inserts a new line (on Win32 platforms).
\r                       Inserts a carriage return.
\t                       Inserts a horizontal tab into the string literal.