4.6.3 字符串(1)
我们已经接触过在System命名空间中定义的、表示C++(www.cppentry.com)/CLI字符串(即Unicode字符组成的字符串)的String类。更准确地说,String类表示由System::Char类型的字符序列组成的字符串。String类对象具备很多强大的功能,从而使字符串的处理非常轻松。我们首先来学习字符串的创建方法。
我们可以像下面这样创建String对象:
- System::String^ saying = L"Many hands make light work.";
变量saying是一个跟踪句柄,它引用被初始化为等号右边字符串的String对象。我们必须始终用跟踪句柄来存储String对象的引用。这里的字符串字面值是宽字符串,因为它的前缀是L。如果我们省略L,则该字面值将包含8位的字符,但编译器可以确保将其转换为宽字符串。
就像数组一样,我们可以使用索引来访问字符串的各个字符,其中第一个字符的索引值是0。下面是输出saying字符串中第三个字符的方法:
- Console::WriteLine(L"The third character
in the string is {0}", saying[2]);
注意,我们只能使用索引值来检索字符串中的字符,而不能以这种方式更新字符串。String对象是固定不变的,因此不能被修改。
通过访问Length属性,我们可以获得字符串中字符的数量。下面这条语句将输出saying字符串的长度:
- Console::WriteLine(L"The string has {0} characters.", saying->Length);
因为saying是跟踪句柄-- 即我们所知道的一种指针,所以必须使用->运算符才能访问Length属性(或该对象的任何其他成员)。当详细研究C++(www.cppentry.com)/CLI类时,我们将了解到更多的属性。
1. 连接字符串
可以使用+运算符来连接字符串,以形成新的String对象。看下面的示例:
- String^ name1 = L"Beth";
- String^ name2 = L"Betty";
- String^ name3 = name1 + L" and " + name2;
执行这些语句之后,name3将包含字符串"Beth and Betty"。注意用+运算符连接String对象和字符串字面值的方法。我们还可以连接String对象和数值或bool值,这些值将在连接操作之前被自动转换为字符串。下面的语句可以说明这一点:
- String^ str = L"Value: ";
- String^ strstr1 = str + 2.5; // Result is new
string "Value: 2.5" - String^ strstr2 = str + 25; // Result is
new string L"Value: 25" - String^ strstr3 = str + true; // Result is new
string L"Value: True"
我们也可以连接String对象和字符,但结果取决于字符的类型:
- char ch = 'Z';
- wchar_t wch = L'Z';
- String^ strstr4 = str + ch; // Result
is new string L"Value: 90" - String^ strstr5 = str + wch; // Result is
new string L"Value: Z"
注释给出了操作的结果。char类型的字符被作为数值对待,因此与字符串连接的是字符Z的代码值。wchar_t字符与String对象中的字符具有相同的类型(Char类型),所以字符Z附加到字符串后面。
不要忘记,String对象是固定不变的,它们一旦创建之后就不能再被修改。这意味着所有表面上在修改String对象的操作实际上都是在创建新的String对象。
String类还定义了一个Join()函数,可用于将数组中存储的多个字符串连接成一个字符串,原来的字符串之间用分隔符分开。下面是将多个姓名连接成一个字符串的方法,各个姓名以逗号分开:
- array<String^>^ names = { L"Jill", L"Ted", L"Mary", L"Eve", L"Bill"};
- String^ separator = L", ";
- String^ joined = String::Join(separator, names);
执行这些语句之后,joined将引用字符串L"Jill, Ted, Mary, Eve, Bill"。分隔符字符串被插入到names数组中原来的各个字符串之间。当然,分隔符字符串可以是任何我们喜欢的内容,比如可以是L"and",这样结果字符串将是L"Jill and Ted and Mary and Eve and Bill"。
下面介绍一个如何处理String对象的完整示例。
试一试:处理字符串
假设有一个整型数组,希望按列对齐输出这些整数。我们不仅要求输出的数值对齐,而且要求列字段足够宽,以容纳数组中最长的数值和列与列之间的间隔。该程序的代码如下:
- // Ex4_18.cpp : main project file.
- // Creating a custom format string
-
- #include "stdafx.h"
-
- using namespace System;
-
- int main(array<System::String ^> ^args)
- {
- array<int>^ values = { 2, 456, 23, -46, 34211, 456, 5609, 112098,
- 234, -76504, 341, 6788, -909121, 99, 10};
- String^ formatStr1 = L"{0," // 1st half of format string
- String^ formatStr2 = L"}"; // 2nd half of format string
- String^ number; // Stores a number as a string
-
- // Find the length of the maximum length value string
- int maxLength = 0; // Holds the maximum length found
- for each(int value in values)
- {
- number = L"" + value; // Create string from value
- if(maxLength<number->Length)
- maxLength = number->Length;
- }
-
- // Create the format string to be used for output
- String^ format = formatStr1 + (maxLength+1) + formatStr2;
-
- // Output the values
- int numberPerLine = 3;
- for(int i = 0 ; i< values->Length ; i++)
- {
- Console::Write(format, values[i]);
- if((i+1)%numberPerLine == 0)
- Console::WriteLine();
- }
- return 0;
- }
该程序的输出如下:
|
2< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> |
456 |
23 |
|
-46 |
34211 |
456 |
|
5609 |
112098 |
234 |
|
-76504 |
341 |
6788 |
|
-909121 |
99 |
10 |