std::string

1、substr( size_type off, size_type count ) 从源串中复制子串

#include <string>
//复制子串
std::string str1("新和xinbingcup");
std::string str_sub = str1.substr( 0, 4 );
//substr( size_type off, size_type count )
//off - 子串起始字符的位置,默认0  count - 子串长度,默认源串长度
//若count超过了父串长度,则子串将延续到源字符串的结尾

 

2、size_type find( char ch, size_type index )    size_type rfind( char ch, size_type index )

     返回源串中字符ch的位置

#include <string>
//size_type find( char ch, size_type index );  正序
//index是源串中查找的基准位置,从0开始数起,默认0
//返回从index开始从左往右第一个ch的位置,找不到返回std::string::npos
//size_type rfind( char ch, size_type index ); 逆序
//index是源串中查找的基准位置,从0开始数起,默认源串长度
//返回从index开始从右往左第一个ch的位置,找不到返回std::string::npos
//std::string::npos 是一个常数,用来表示不存在的位置,一般是-1
std::string str1("xinbingicup");
int rr = str1.rfind('i');//7
int tt = str1.find('i');//1
int o = std::string::npos;//-1

 

posted @ 2014-09-11 11:35  想什么想  阅读(179)  评论(0编辑  收藏  举报