Exhaust character

Asked

Viewed 954 times

5

I’m having trouble understanding the escape characters, I already know that the \n break line in a string, but the other characters could not understand.

Ex: \a \b \f \r \t \v, found the documentation from Microsoft and does not have many examples.

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

  • I once did that : Cola 1, Cola 2, not all languages implement equal, and when I did it went to REGEX. Use as basis, not fact.

  • Good afternoon to you, Samuel. Check here: http://www.codeproject.com/Articles/371232/Escaping-in-Csharp-characters-strings-string-forma Hugs and good studies!

2 answers

5


Suppose you are making a small program to test these escape characters, and want to print on the screen. Good examples to get started 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 escape characters is to tell the interpreter:

A quote or an apostrophe with a backslash in the front should not be treated as special symbols, but rather as its literal representation, or the reverse: given the symbol in its escape pattern, it becomes a special meaning.

The escape sequence therefore nullifies the special purpose that a symbol has in a language, or otherwise expresses a symbol whose representation is abstract or ambiguous in a given context, such as space and tab symbols (\s and \t, respectively).

Suppose now a String completely "empty" (but which is actually filled with spaces and tabs) and that I want to count how many spaces and how many tabs exist within it. A way to do it 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 the example, 23) and \t counts only those that are actual tabs (16). Try switching in the example.

Cases such as \a and \b apply well when waiting for user keyboard commands or trigger the hardware (specific case of \a).

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

  • Related : http://answall.com/questions/110701/o-que-significa-shortcut-s-nas-regex

  • I understand, but in case ?

  • 1

    @samuelrvg, the question has more to do with text itself than with c#; \f = formfeed , changes page in printers and pagers; \r\n = line changes in windows ; \v = vertical tab, most rarely used to vertically separate pieces of text

2

Escape sequence is a combination of characters consisting of a counter bar \ followed by a letter or combinations of digits. It is used in the representation of control characters such as CR, apostrophe, etc..

Escape sequences allow sending non-graphic control characters to printing devices. The character ESC is often used as the starting character of a control command for the printer or for a video terminal, i.e., escape sequence.

Below is a summary of the most used special characters in programming:

\a - BEL Bell
\b - BS (ascii) BackSpace
\f - FF Formfeed
\n - LF NewLine
\r - CR Carriage Return
\t - HT Horizontal Tabulation
\v - VT (ascii) Vertical Tabulation
\' - Apóstrofe (Single quotation mark)
\" - Aspas (Double quotation mark)
\\ - Contrabarra
\ooo - Caracter ASCII em notação octal
\xhhh - Caracter ASCII em notação hexadecimal

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.