find_first_not_of
原型:
#include <string>
size_type find_first_not_of( const string& str, size_type index = 0 ) const;
size_type find_first_not_of( const Char* str, size_type index = 0 ) const;
size_type find_first_not_of( const Char* str, size_type index, size_type num ) const;
size_type find_first_not_of( Char ch, size_type index = 0 ) const;
函数find_first_not_of()功能如下:
-
返回在字符串中首次出现的不匹配str任何字符的首字符索引, 从index开始搜索, 如果全部匹配则返回string::npos(string::npos就是串的结尾)
-
从index开始起搜索当前字符串, 查找其中与str前num个字符中的任意一个都不匹配的序列, 返回满足条件的第一个字符索引, 否则返回string::npos,
-
返回在当前字符串中第一个不匹配ch字符的索引, 从index开始搜索, 没用收获则返回string::npos.
例如, 下面的代码搜索一段文本中第一个不是小写字母, ‘ ’, ‘,’, '-'四者之一的字符:
string lower_case = "abcdefghijklmnopqrstuvwxyz ,-"; string str = "this is the lower-case part, AND THIS IS THE UPPER-CASE PART"; cout << "first non-lower-case letter in str at: " << str.find_first_not_of(lower_case) << endl;
运行之后, find_first_not_of()发现第一个大写字母出现在索引为29的位置, 并且产生如下输出:
first non-lower-case letter in str at: 29
find
原型:
#include <string>
size_type find( const string& str, size_type index = 0 ) const;
size_type find( const Char* str, size_type index = 0 ) const;
size_type find( const Char* str, size_type index, size_type length ) const;
size_type find( Char ch, size_type index = 0 ) const;
find在下列情况之一返回:
-
在当前字符串中从index开始搜索并第一次发现str, 如果没有找到则返回string::npos
-
在当前字符串中从index开始搜索并且str的前length个字符出现在字符串中, 如果没有找到则返回string::npos.
例如:
string str1( "Alpha Beta Gamma Delta" ); string::size_type loc1 = str1.find( "Omega", 0 ); string::size_type loc2 = str1.find( "Gamma", 0 ); if( loc1 != string::npos ) { cout << "Found Omega at " << loc1 << endl; } else { cout << "Didn't find Omega" << endl; } if( loc2 != string::npos ) { cout << "Found Gamma at " << loc2 << endl; } else { cout << "Didn't find Gamma" << endl; }
结果是
Didn't find Omega
Found Gamma at 11
find_first_of
原型:
#include <string>
size_type find_first_of( const string &str, size_type index = 0 ) const;
size_type find_first_of( const Char* str, size_type index = 0 ) const;
size_type find_first_of( const Char* str, size_type index, size_type num ) const;
size_type find_first_of( Char ch, size_type index = 0 ) const;
函数find_first_of功能如下:
-
返回在当前字符串中第一个匹配str中任意字符的字符索引, 从index开始搜索, 都不匹配则返回string::npos,
-
从index处开始搜索当前字符串, 查找匹配str前num个字符中任意一个的字符, 返回在字符串中找到的字符的索引, 都不匹配则返回string::npos,
-
从index开始搜索, 返回当前字符串中第一个匹配ch的字符索引, 未发现则返回string::npos.
例如, 下面的代码利用find_first_of将'*'替换字符串中的所有元音字母:
string str = "In this house, we obey the laws of thermodynamics!"; string::size_type found = str.find_first_of("aeiouAEIOU"); while( found != string::npos ) { str[found] = '*'; found = str.find_first_of("aeiouAEIUO",found+1); } cout << str << '\n'; // displays "*n th*s h**s*, w* *b*y th* l*ws *f th*rm*dyn*m*cs!"
find_last_not_of
原型:
#include
size_type find_last_not_of( const string& str, size_type index = npos ) const;
size_type find_last_not_of( const Char* str, size_type index = npos ) const;
size_type find_last_not_of( const Char* str, size_type index, size_type num ) const;
size_type find_last_not_of( Char ch, size_type index = npos ) const;
函数find_last_not_of()功能如下:
-
返回当前字符串中最后一个与str中任意字符都不匹配的字符索引, 逆向的搜索工作从index开始, 全部匹配则返回string::npos,
-
返回当前字符串中最后一个与str中前num个字符的任意一个都不匹配的字符的索引, 从index开始进行逆向搜索, 如果都匹配则返回string::npos,
-
返回当前字符串中最后一个与ch不匹配的字符索引, 从index开始逆向搜索, 都匹配则返回string::npos.
例如, 下面的代码搜索字符串中最后一个非小写字母的字符:
string lower_case = "abcdefghijklmnopqrstuvwxyz"; string str = "abcdefgABCDEFGhijklmnop"; cout << "last non-lower-case letter in str at: " << str.find_last_not_of(lower_case) << endl;
代码产生下面的输出:
last non-lower-case letter in str at: 13
find_last_of
原型:
#include <string>
size_type find_last_of( const string& str, size_type index = npos ) const;
size_type find_last_of( const Char* str, size_type index = npos ) const;
size_type find_last_of( const Char* str, size_type index, size_type num ) const;
size_type find_last_of( Char ch, size_type index = npos ) const;
函数find_last_of()功能如下:
-
从index开始进行逆向搜索, 返回字符串中遇到的第一个与str中任意一个字符相匹配的字符索引, 如果都不匹配则返回string::npos,
-
从index开始进行逆向搜索, 返回字符串中遇到的第一个与str前导num个字符中任意一个像匹配的字符索引, 如果都不匹配则返回string::npos,
-
从index开始进行逆向搜索, 返回字符串中遇到的第一个与ch相匹配的字符索引, 如果没有匹配字符则返回string::npos.
rfind
原型:
#include <string>
size_type rfind( const string& str, size_type index = npos ) const;
size_type rfind( const Char* str, size_type index = npos ) const;
size_type rfind( const Char* str, size_type index, size_type num ) const;
size_type rfind( Char ch, size_type index = npos ) const;
函数rfind()返回在字符串中最后一次遇到str的位置, 它从index处开始进行逆向搜索:
-
如果没有找到则返回string::npos
-
从字符串首部的相对位移index处开始搜索
-
至多搜索num个字符
例如, 下面的代码, 第一次调用rfind()返回string::npos, 目标字并不在字符串的前8个字符中. 然而, 第二次调用返回9, 因为目标字就在字符串的前20个字符中.
string::size_type loc; string s = "My cat's breath smells like cat food."; loc = s.rfind( "breath", 8 ); cout << "The word breath is at index " << loc << endl; loc = s.rfind( "breath", 20 ); cout << "The word breath is at index " << loc << endl;