用str中的num个字符替换本字符串中的字符,从index开始
用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
用str中的num个字符(从index开始)替换本字符串中的字符
用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
用num2个ch字符替换本字符串中的字符,从index开始
用str中的字符替换本字符串中的字符,迭代器start和end指示范围
用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围,
用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.
例如,以下代码显示字符串"They say he carved it himself...find your soul-mate, Homer."
string s = "They say he carved it himself...from a BIGGER spoon";
string s2 = "find your soul-mate, Homer.";
rfind
语法:
size_type rfind( const basic_string &str, size_type index );
size_type rfind( const char *str, size_type index );
size_type rfind( const char *str, size_type index, size_type num );
size_type rfind( char ch, size_type index );
rfind()函数:
返回最后一个与str中的某个字符匹配的字符,从index开始查找。如果没找到就返回string::npos
返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符。如果没找到就返回string::npos
返回最后一个与ch匹配的字符,从index开始查找。如果没找到就返回string::npos
例如,在下列代码中第一次调用rfind()返回string::npos,因为目标词语不在开始的8个字符中。然而,第二次调用返回9,因为目标词语在开始的20个字符之中。
int 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;
string s("So, you like donuts, eh? Well, have all the donuts in the world!");
cout << "The original string is '" << s << "'" << endl;
s.erase( 50, 14 );
cout << "Now the string is '" << s << "'" << endl;
s.erase( 24 );
cout << "Now the string is '" << s << "'" << endl;
s.erase();
cout << "Now the string is '" << s << "'" << endl;
将显示
The original string is 'So, you like donuts, eh? Well, have all the donuts in the world!'
Now the string is 'So, you like donuts, eh? Well, have all the donuts'
Now the string is 'So, you like donuts, eh?'
Now the string is ''