Exhaust characters

4

I'm having trouble understanding the escape characters, I already know that the \n line breaks in a string, but the other characters I can not understand.

Ex: \a \b \f \r \t \v , I joined the site link and does not have many examples.

I would like to see some examples in C # and explanations if possible.

    
asked by anonymous 04.03.2016 / 04:44

1 answer

4

Suppose you are running a small program to test these escape characters, and want to print on the screen. Good examples to begin with are \" and \' .

I did the following Fiddle :

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("\"");
        Console.WriteLine("\'");
    }
}

The result will be:

"
'

The idea of escaping characters is to tell the interpreter:

  

A quotation mark or an apostrophe with a backslash in front should not be treated as special symbols, but rather as its literal representation, or else the reverse: given the symbol in the escape sequence, it becomes a special sense .

The escape sequence thus negates the special purpose that a symbol possesses in a language, or otherwise expresses a symbol whose representation is abstract or ambiguous in a given context, such as space and tabulation symbols (% with% and% with%, respectively).

Suppose now a completely empty% (which is actually prepended by spaces and tabs ) and that I want to count how many spaces and how many tabs exist inside it. One way to do this is:

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        String tabsEEspacos = "                                                             ";
        Console.WriteLine(Regex.Matches(tabsEEspacos, @"\s").Count);
    }
}

\s counts all spaces (in example, 23) and \t counts only those that are fact tabs (16). Try swapping in the example.

Cases such as String and \s apply well when waiting for user keyboard commands or to trigger hardware (specific case of \t ).

ASCII and Unicode escape sequences are useful for converting formats, from one to the other, for example.

    
04.03.2016 / 05:43