Working with Strings
Escape Characters
Escape characters such as "\n" (new line) and "\t" (tab) can be included in strings. The line:
string hello = "Hello\nWorld!"; |
is the same as:
Hello
World!
If you want to include a backward slash, it must be preceded with another backward slash. The following string:
string filePath = "\\\\My Documents\\"; |
is actually the same as:
\\My Documents\
Работа со строками
Escape-знаки
Строки могут содержать escape-знаки, такие как "\n" (новая строка) и "\t" (табуляция). Строка:
string hello = "Hello\nWorld!";
эквивалентна строке:
Hello
World!
Если требуется добавить в строку обратную косую черту, перед ней нужно поставить еще одну обратную косую черту. Следующая строка
string filePath = "\\\\My Documents\\";
эквивалентна строке
\\My Documents\
The @ Symbol
The @ symbol specifies that escape characters and line breaks should be ignored when the string is created. The following two strings are therefore identical:
string p1 = "\\\\My Documents\\My Files\\"; string p2 = @"\\My Documents\My Files\"; |
ToString()
The C# built-in data types all provide the ToString method, which converts a value to a string. This method can be used to convert numeric values into strings, like this:
int year = 1999; string msg = "Eve was born in " + year.ToString(); System.Console.WriteLine(msg); // outputs "Eve was born in 1999" |
Символ @
Символ @ указывает, что при создании строки следует игнорировать escape-знаки и переносы строки. Следующие две строки являются идентичными.
string p1 = "\\\\My Documents\\My Files\\";string p2 = @"\\My Documents\My Files\";ToString()
У всех встроенных типов данных C# есть метод ToString, преобразующий значение в строку. Этот метод может быть использован для преобразования числовых значений в строки следующим образом.
int year = 1999;string msg = "Eve was born in " + year.ToString();System.Console.WriteLine(msg); // outputs "Eve was born in 1999"Дата добавления: 2022-05-27; просмотров: 110;