C++中的string类的部分函数的使用

来源:互联网 发布:淘宝仿真娃娃 编辑:程序博客网 时间:2024/05/17 09:12

#include "iostream"
#include "string"
using namespace std;
int main()
{
 string str1("abcdefghijk");
 string str2("cde");
 int loc;
 loc=str1.find("e");     //在str1中寻找e
 if(loc!=-1)
  cout<<"find e in "<<loc<<endl;
 else
  cout<<"cannot find e"<<endl;

 loc=str1.find("e",5);    //在str1中从第6个字符开始寻找e
 if(loc!=-1)
  cout<<"find e in "<<loc<<endl;
 else
  cout<<"cannot find e"<<endl;

 loc=str1.find(str2);   //在str1中寻找str2串
 if(loc!=-1)
  cout<<"find str2 in "<<loc<<endl;
 else
  cout<<"cannot find str2"<<endl;

 loc=str1.find(str2,3);    //在str1中从第4个字符开始寻找str2串
 if(loc!=-1)
  cout<<"find str2 in "<<loc<<endl;
 else
  cout<<"cannot find str2"<<endl;
 return 0;
}

原创粉丝点击