请问哪个函数可以检查一个string中是否有某个substring?

mituzhishi 2004-05-05 03:33:26
请问哪个函数可以检查一个string中是否有某个substring?
...全文
82 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
supergt2001 2004-05-11
  • 打赏
  • 举报
回复
最好用这个:
strstr();
xjl999 2004-05-06
  • 打赏
  • 举报
回复
去看MSDN.
mituzhishi 2004-05-05
  • 打赏
  • 举报
回复
谢谢心宇!!
minghuitian 2004-05-05
  • 打赏
  • 举报
回复
up
morisecue 2004-05-05
  • 打赏
  • 举报
回复
我一直以为只有Java里有String类,
Java里有findIndex方法,可以找到所需子串的下标
freefalcon 2004-05-05
  • 打赏
  • 举报
回复
晕,俺那句话音用至辣子鸡丁,俺不是要分的啦

getline不是string的成员
标准C++中的原型之一为istream& getline(istream& is, string str, char delimiter);

所以用法为
ifstream infile("***");
string str;
while(getline(infile, str, '\n'))
{
cout << str;
}

我觉得你这样一个个函数的问不是办法,找一本C++的基础书看看更好一些
mituzhishi 2004-05-05
  • 打赏
  • 举报
回复
回心宇:
“show me the money! haha
black sheep wall?“

只要肯帮忙,分是不会少的
mituzhishi 2004-05-05
  • 打赏
  • 举报
回复
这样的用getline()编译器通不过:

string temp;
infile.getline(temp);
freefalcon 2004-05-05
  • 打赏
  • 举报
回复
show me the money! haha
black sheep wall?
mituzhishi 2004-05-05
  • 打赏
  • 举报
回复
顺便讲一下getline()的用法;举几个例子最好了。谢!
rorot 2004-05-05
  • 打赏
  • 举报
回复
string substring = "ABC";
string str = "sdfasjABCfdslfABC";
if ( str.find( substring.c_str(), 0 ) != -1 )

else
没有
Jinhao 2004-05-05
  • 打赏
  • 举报
回复
够详细了吧~~嘿嘿
int main(){
string str="show me the money show you the bomb";
string whatdoyoulookfor="show";
int index=0;
while(1){
index = str.find(whatdoyoulookfor,index);
if(index!=-1){
cout<<whatdoyoulookfor<<" is in "<<str<<" at "<<index<<endl;
index+=whatdoyoulookfor.length();
}else{
break;
}
}
}
freefalcon 2004-05-05
  • 打赏
  • 举报
回复
比如
string s("1234abc56");
if(s.find("abc") != string::npos)
{
cout << "ok, found!";
}
mituzhishi 2004-05-05
  • 打赏
  • 举报
回复
能不能说的再详细一些?
freefalcon 2004-05-05
  • 打赏
  • 举报
回复
用string的find系列函数

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧