设为首页 加入收藏

TOP

4.6.3 字符串(4)
2013-10-07 12:32:45 来源: 作者: 【 】 浏览:61
Tags:4.6.3 字符串

4.6.3  字符串(4)

使用与以前相同的word和words字符串,该代码段将产生相同的输出。因为LastIndexOf()往回搜索,所以开始的索引值是words->Length-1,对应字符串中最后一个字符。现在如果发现word出现时,我们必须将index减1,以便下次往回搜索时从当前word之前的那个字符开始。如果word正好出现在words的头部-- 即索引位置是0,则使index减1的结果是-1,这不是LastIndexOf()函数的合法参数,因为搜索的开始位置必须总在字符串之内。在循环条件中检查index是不是负值可以避免上述情况发生。如果&&运算符的右操作数是false,则不再计算左操作数。

最后一个需要提及的搜索函数是IndexOfAny(),它在字符串中搜索作为参数提供给它的、<wchar_t>类型的数组中任何字符的第一个实例。与IndexOf()函数类似,IndexOfAny()函数也有从字符串的头部或从指定的索引位置开始搜索的版本。下面是使用IndexOfAny()函数的一个完整示例。

试一试:搜索任意一组字符

该程序在字符串中搜索标点符号:

  1. // Ex4_19.cpp : main project file.  
  2. // Searching for punctuation  
  3.  
  4. #include "stdafx.h"  
  5.  
  6. using namespace System;  
  7.  
  8. int main(array<System::String ^> ^args)  
  9. {  
  10. array<wchar_t>punctuation = {L'"', L'\'',
    L'.', L',', L':', L';', L'!',   
  11. L' '};  
  12. String^ ssentence = L"\"It's chilly in here\", 
    the boy's mother said   
  13. coldly.";  
  14.  
  15. // Create array of space characters same length as sentence  
  16. array<wchar_t>indicators = gcnew array<wchar_t>
    (sentence->Length){L' '};  
  17.  
  18. int index = 0;      // Index of character found  
  19. int count = 0;      // Count of punctuation characters  
  20. while((index = sentence->IndexOfAny(punctuation, index)) >= 0)  
  21. {  
  22. indicators[index] = L'^'; // Set marker  
  23. ++index;    // Increment to next character  
  24. ++count;    // Increase the count  
  25. }  
  26. Console::WriteLine(L"There are {0} punctuation characters in the   
  27. string:",count);  
  28. Console::WriteLine(L"\n{0}\n{1}", sentence, 
    gcnew String(indicators));  
  29. return 0;  

该示例应该生成下面的输出:

  1. There are 6 punctuation characters in the string:  
  2.  
  3. "It's chilly in here", the boy's mother said coldly.  
  4. ^ ^ ^^ ^ ^ 

示例说明

我们首先创建包含要查找的字符的数组,还要创建将被搜索的字符串:

  1. array<wchar_t>punctuation = {L'"', L'\'', L'.', 
    L',', L':', L';', L'!', L' '};  
  2. String^ ssentence = L"\"It's chilly in here\", 
    the boy's mother said coldly."; 

注意,必须使用转义序列来指定单引号字符,因为单引号在字符字面值中是定界符。我们可以在字符字面值中显式地使用双引号,因为在该上下文中不存在将其解释为定界符的风险。

接着定义一个字符数组,将该数组的元素初始化成空格符:

  1. array<wchar_t>indicators = gcnew array<
    wchar_t>(sentence->Length){L' '}; 

该数组的元素数与sentence字符串中的字符数一样多。我们将在输出中用该数组来标记标点符号在sentence字符串中出现的位置。只要找到一个标点符号,就将适当的数组元素修改为'^'。注意在数组说明后面的大括号中用一个初值初始化所有数组元素的方式。

搜索发生在while循环中:

  1. while((index = sentence->IndexOfAny(punctuation, index)) >= 0)  
  2. {  
  3. indicators[index] = L'^';       // Set marker  
  4. ++index;        // Increment to next character  
  5. ++count;        // Increase the count  

循环条件本质上与我们在前面的代码段中看到的相同。在循环体内,我们将index位置的数组元素初始化为'^'字符,然后使index加1,为下次执行循环作好准备。当该循环结束时,count包含找到的标点符号的数量,而indicators将在sentence中这些字符所在的位置上包含'^'字符。

由下面这两条语句产生输出:

  1. Console::WriteLine(L"There are {0} punctuation characters in the string:",  
  2. count);  
  3. Console::WriteLine(L"\n{0}\n{1}" sentence, gcnew String(indicators)); 

第二条语句通过将indicators数组传递给String类的构造函数,从而在堆上根据该数组创建一个新的String对象。当被调用时,类的构造函数将创建本类的对象。当我们研究如何定义自己的类时,将学习与构造函数有关的更多内容。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇下载与学习JSON的类库的图记录 下一篇4.6.3 字符串(3)

评论

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