设为首页 加入收藏

TOP

4.6.3 字符串(1)
2013-10-07 12:32:18 来源: 作者: 【 】 浏览:74
Tags:4.6.3 字符串

4.6.3  字符串(1)

我们已经接触过在System命名空间中定义的、表示C++(www.cppentry.com)/CLI字符串(即Unicode字符组成的字符串)的String类。更准确地说,String类表示由System::Char类型的字符序列组成的字符串。String类对象具备很多强大的功能,从而使字符串的处理非常轻松。我们首先来学习字符串的创建方法。

我们可以像下面这样创建String对象:

  1. System::String^ saying = L"Many hands make light work."

变量saying是一个跟踪句柄,它引用被初始化为等号右边字符串的String对象。我们必须始终用跟踪句柄来存储String对象的引用。这里的字符串字面值是宽字符串,因为它的前缀是L。如果我们省略L,则该字面值将包含8位的字符,但编译器可以确保将其转换为宽字符串。

就像数组一样,我们可以使用索引来访问字符串的各个字符,其中第一个字符的索引值是0。下面是输出saying字符串中第三个字符的方法:

  1. Console::WriteLine(L"The third character
    in the string is {0}", saying[2]); 

注意,我们只能使用索引值来检索字符串中的字符,而不能以这种方式更新字符串。String对象是固定不变的,因此不能被修改。

通过访问Length属性,我们可以获得字符串中字符的数量。下面这条语句将输出saying字符串的长度:

  1. Console::WriteLine(L"The string has {0} characters.", saying->Length); 

因为saying是跟踪句柄-- 即我们所知道的一种指针,所以必须使用->运算符才能访问Length属性(或该对象的任何其他成员)。当详细研究C++(www.cppentry.com)/CLI类时,我们将了解到更多的属性。

1. 连接字符串

可以使用+运算符来连接字符串,以形成新的String对象。看下面的示例:

  1. String^ name1 = L"Beth";  
  2. String^ name2 = L"Betty";  
  3. String^ name3 = name1 + L" and " + name2; 

执行这些语句之后,name3将包含字符串"Beth and Betty"。注意用+运算符连接String对象和字符串字面值的方法。我们还可以连接String对象和数值或bool值,这些值将在连接操作之前被自动转换为字符串。下面的语句可以说明这一点:

  1. String^ str = L"Value: ";  
  2. String^ strstr1 = str + 2.5;       // Result is new 
    string "Value: 2.5"  
  3. String^ strstr2 = str + 25;            // Result is 
    new string L"Value: 25"  
  4. String^ strstr3 = str + true;  // Result is new 
    string L"Value: True" 

我们也可以连接String对象和字符,但结果取决于字符的类型:

  1. char ch = 'Z';  
  2. wchar_t wch = L'Z';  
  3. String^ strstr4 = str + ch;            // Result 
    is new string L"Value: 90"  
  4. String^ strstr5 = str + wch;       // Result is 
    new string L"Value: Z" 

注释给出了操作的结果。char类型的字符被作为数值对待,因此与字符串连接的是字符Z的代码值。wchar_t字符与String对象中的字符具有相同的类型(Char类型),所以字符Z附加到字符串后面。

不要忘记,String对象是固定不变的,它们一旦创建之后就不能再被修改。这意味着所有表面上在修改String对象的操作实际上都是在创建新的String对象。

String类还定义了一个Join()函数,可用于将数组中存储的多个字符串连接成一个字符串,原来的字符串之间用分隔符分开。下面是将多个姓名连接成一个字符串的方法,各个姓名以逗号分开:

  1. array<String^>names = { L"Jill", L"Ted", L"Mary", L"Eve", L"Bill"};  
  2. String^ separator = L", ";  
  3. 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对象的完整示例。

试一试:处理字符串

假设有一个整型数组,希望按列对齐输出这些整数。我们不仅要求输出的数值对齐,而且要求列字段足够宽,以容纳数组中最长的数值和列与列之间的间隔。该程序的代码如下:

  1. // Ex4_18.cpp : main project file.  
  2. // Creating a custom format string  
  3.  
  4. #include "stdafx.h"  
  5.  
  6. using namespace System;  
  7.  
  8. int main(array<System::String ^> ^args)  
  9. {  
  10. array<int>values = { 2, 456, 23, -46, 34211, 456, 5609, 112098,  
  11. 234, -76504, 341, 6788, -909121, 99, 10};  
  12. String^ formatStr1 = L"{0,"         // 1st half of format string  
  13. String^ formatStr2 = L"}";          // 2nd half of format string  
  14. String^ number;     // Stores a number as a string  
  15.  
  16. // Find the length of the maximum length value string  
  17. int maxLength = 0;          // Holds the maximum length found  
  18. for each(int value in values)  
  19. {  
  20. number = L"" + value;   // Create string from value  
  21. if(maxLength<number->Length)  
  22. maxLength = number->Length;  
  23. }  
  24.  
  25. // Create the format string to be used for output  
  26. String^ format = formatStr1 + (maxLength+1) + formatStr2;  
  27.  
  28. // Output the values  
  29. int numberPerLine = 3;  
  30. for(int i = 0 ; i< values->Length ; i++)  
  31. {  
  32. Console::Write(format, values[i]);  
  33. if((i+1)%numberPerLine == 0)  
  34. Console::WriteLine();  
  35. }  
  36. 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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇4.6.3 字符串(2) 下一篇4.6.5 内部指针

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: