1.string.find函数
#include <iostream>
#include <string>
using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { string s;
string str;
char *p = "abc"; int index; getline(cin,s);
cout<<"s="<<s<<endl; getline(cin,str);
cout<<"str="<<str<<endl; index = s.find(str);
cout<<"char index="<<index<<endl; index = s.find(p);
cout<<"string index="<<index<<endl; return ;
}
2. 取子串
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std; //输入2015-10-22
int riqi(string str){
int i = ;
int year;
int month;
int day;
int i_start,i_end;
string tmp; i_start = str.find("-");//第一个'-'
tmp = str.substr(,i_start);//取从0下标开始的i_start个字符
year = atoi(tmp.c_str());//tmp.c_str()转化为C语言中字符串指针 i_end = str.rfind("-");//最后一个'-'
tmp = str.substr(i_start+,i_end - i_start);
month = atoi(tmp.c_str()); tmp = str.substr(i_end+);
day = atoi(tmp.c_str());
cout<<year<<" "<<month<<' '<<day<<endl;
//输出的结果为 2015 10 22
return ;
}