Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

7.1.3Introduction to Strings

The data types we introduced in Lesson 2 each supports a precise type of symbols to make up its value. For example, an integer must contain only digits or characters that can be evaluated to a number. A decimal number can combine only digits and a period for its value. A character can be only one symbol. We also saw that there were precise ways to initialize variables of these types. A string is one or more characters or symbols considered as an entity. The symbols can be digits only. It can be a combination of digits and letters. It can also combine digits, letters, and non-readable characters. This type of definition doesn't fit any of the data types we have seen so far, not even the Object. To be able to consider such a combination as a (whole) value, there are some techniques you must use. Overall, such a combination is considered a string.

In the C++/CLI and the .NET Framework, a string is created using the String class. You can declare it as a handle. Here is an example:

using namespace System;

int main()

{

String ^ Whatever;

return 0;

}

After declaring the variable, you can initialize it. Because a string can be used to represent any type significant or insignificant value, there are techniques you must use to initialize it. The fundamental technique of initializing a string is to include its value in double-quotes. Here is an example:

using namespace System;

int main()

{

String ^ Whatever = "KhjK526Hh6";

return 0;

}

To display the value of a String object, you can pass it to Write() or WriteLine(). Here is an example:

using namespace System;

int main()

{

String ^ Whatever = "KhjK526Hh6";

Console::WriteLine(Whatever);

return 0;

}

This would produce:

KhjK526Hh6

You can also include one or more escape sequences in a String object. Here is an example:

using namespace System;

int main()

{

String ^ WhiteHouse = "The White House\n";

Console::WriteLine(WhiteHouse);

return 0;

}

A string can be of almost any length, at least you are allowed to create long strings. When initializing a long string, you can span its value on various lines. Each line must have its pair of quotes. Here is an example:

using namespace System;

int main()

{

String ^ WhiteHouse = "The White House"

"1600 Pennsylvania Avenue"

"Washington, DC";

return 0;

}

As mentioned already, you can put escape sequences anywhere inside of the string. The compiler would find and interpret them appropriately. Here are examples:

using namespace System;

int main()

{

String ^ WhiteHouse = "The White House\n"

"1600 Pennsylvania Avenue\n"

"Washington, DC";

Console::WriteLine(WhiteHouse);

return 0;

}

This would produce:

The White House

1600 Pennsylvania Avenue

Washington, DC

Another technique you can use to span a string on various lines with one pair of double-quotes is to end each line with a backslash. Here is an example:

int main()

{

String ^ str = "A Soho, \

dans Londres";

Console::WriteLine(str);

return 0;

}

A string as those we have used so far is considered a regular string. Notice that our strings use only regular characters of a Latin-based language. The C++ language also supports wide strings of the Unicode. To specify that your String value supports Unicode, you can precede its initialization with L. Here is an example:

using namespace System;