C++ string类型详解(二)

2014-11-24 11:53:47 · 作者: · 浏览: 1
, size_type num, const basic_string &str );//用str中的num个字符替换本字符串中的字符,从index开始
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
size_type num2 );//用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
basic_string &replace( size_type index, size_type num, const char *str );//用str中的num个字符(从index开始)替换本字符串中的字符
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );//用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
basic_string &replace( size_type index, size_type num1, size_type num2, char ch );//用num2个ch字符替换本字符串中的字符,从index开始,num1个字符
basic_string &replace( iterator start, iterator end, const basic_string &str );//用str中的字符替换本字符串中的字符,迭代器start和end指示范围
basic_string &replace( iterator start, iterator end, const char *str );//用str替换本字符串中的内容,迭代器start和end指示范围
basic_string &replace( iterator start, iterator end, const char *str, size_type num );//用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围
basic_string &replace( iterator start, iterator end, size_type num, char ch );//用num个ch字符替换本字符串中的内容,迭代器start和end指示范围

查找
一下各种find函数十分相似,所以就不一一注释了
返回值:如果没找到则返回string::npos

find:
size_type find( const basic_string &str, size_type index );//返回str在字符串中第一次出现的位置(从index开始查找)
size_type find( const char *str, size_type index );//返回str在字符串中第一次出现的位置(从index开始查找)
size_type find( const char *str, size_type index, size_type length );//返回str在字符串中第一次出现的位置(从index开始查找,长度为length)
size_type find( char ch, size_type index );//返回字符ch在字符串中第一次出现的位置(从index开始查找)

find_first_of:查找在字符串中第一个与str中的某个字符匹配的字符
size_type find_first_of( const basic_string &str, size_type index = 0 );
size_type find_first_of( const char *str, size_type index = 0 );
size_type find_first_of( const char *str, size_type index, size_type num );
size_type find_first_of( char ch, size_type index = 0 );

find_first_not_of:在字符串中查找第一个与str中的字符都不匹配的字符
size_type find_first_not_of( const basic_string &str, size_type index = 0 );
size_type find_first_not_of( const char *str, size_type index = 0 );
size_type find_first_not_of( const char *str, size_type index, size_type num );
size_type find_first_not_of( char ch, size_type index = 0 );

find_last_of:在字符串中查找最后一个与str中的某个字符匹配的字符
size_type find_last_of( const basic_string &str, size_type index = npos );
size_type find_last_of( const char *str, size_type index = npos );
size_type find_last_of( const char *str, size_type index, size_type num );
size_type find_last_of( char ch, size_type index = npos );

find_last_not_of:在字符串中查找最后一个与str中的字符都不匹配的字符
size_type find_last_not_of( const basic_string &str, size_type index = npos );
size_type find_last_not_of( const char *str, size_type index = npos);
size_type find_last_not_of( const char *str, size_type index, size_type num );
size_type find_last_not_of( char ch, size_type index = npos );

rfind函数
size_type rfind( const basic_string &str, size_type index );//返回最后一个与str中的某个字符匹配的字符,从index开始查找
size_type rfind( const char *str, size_type index );//返回最后一个与str中的某个字符匹配的字符,从index开始查找
size_type rfind( const char *str, size_type index, size_type num );//返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符
size_type rfind( char ch, size_type index );//返回最后一个与ch匹配的字符,从index开始查找

at函数
reference at( size