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.
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.
– Guilherme Lautert
Good afternoon to you, Samuel. Check here: http://www.codeproject.com/Articles/371232/Escaping-in-Csharp-characters-strings-string-forma Hugs and good studies!
– Rafael Santos