C++中判断字符串A中是否包含字符串B

pengxincz 2010-02-28 03:52:17
C++中判断字符串A中是否包含字符串B

两个类型是string
或者一个CString,另一个string

c#的表达方式是 if(str1.indexof(str2) != -1)
求C++的表达方式
strstr()函数不知道怎么控制string
谢谢
...全文
29085 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lodonsnow 2012-09-13
  • 打赏
  • 举报
回复
围观一下, 正在求这个用法
G.0504 2011-07-29
  • 打赏
  • 举报
回复
求答案,围观
2010-02-28
  • 打赏
  • 举报
回复
       size_type find(const basic_string<charT,traits,Allocator>& str,
size_type pos = 0) const;
Effects: Determines the lowest position xpos, if possible, such that both of the following conditions
obtain:
— pos <= xpos and xpos + str.size() <= size();
— at(xpos+I) == str.at(I) for all elements I of the string controlled by str.

Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos.

Notes: Uses traits::eq().

jixingzhong 2010-02-28
  • 打赏
  • 举报
回复
函数名称: strstr
函数原型: char* strstr(char* str1,char* str2);
函数功能: 找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)
函数返回: 返回该位置的指针,如找不到,返回空指针
参数说明:
所属文件: <string.h>

#include <stdio.h>
#include <string.h>
int main()
{
char *str1="Open Watcom C/C++",*str2="Watcom",*ptr;
ptr=strstr(str1,str2);
printf("The substring is:%s\n",ptr);
return 0;
}
mzlogin 2010-02-28
  • 打赏
  • 举报
回复 1
在BS大牛的书中有一句是……“各种查找子串的函数多得令人有些手足无措”。

可以利用标准库中提供的算法来解决这个问题,
也即6楼所说的find。
find函数有个版本是在一个字符串中查找是否存在另一子串,若存在则返回其找到的第一个子串的首字符下标,若不存在则返回一个很大超出字符串长度的数。一个版本可以为:
#include <iostream> 
#include <string>
using namespace std;

int main()
{
string s1 = "abcde";
string s2 = "cd";
string s3 = "dc";
string::size_type i1 = s1.find(s2);
string::size_type i2 = s1.find(s3);

cout << i1 << endl;
cout << i2 << endl;
return 0;
}

运行结果为
2
4294967295
要进行判断采用6楼的操作即可。
dyw 2010-02-28
  • 打赏
  • 举报
回复
Qt: QString也有indexOf这个方法
stardust20 2010-02-28
  • 打赏
  • 举报
回复 1
string的话可以用find函数
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s="golden global view";
string r="new";
if (s.find(r) < s.length())
{
cout<<"s中包含r"<<endl;
}
else
{
cout<<"s中不包含r"<<endl;
}
return 0;
}
chaoxiaoshuai 2010-02-28
  • 打赏
  • 举报
回复
#include"stdio.h"
#include"iostream.h"
#include"string.h"
void mian()
{
char a[81],b[81];
int i=0,j,na,nb,flag;
cout < <"Input string a:" < <endl;
gets(a);
cout < <"Input string b:" < <endl;
gets(b);
na=strlen(a);nb=strlen(b);
flag=1;
for(i=0;na-i>=nb;i++)
{
flag=-2;
for(j=0;j <nb;j++)
if(a[i+j]!=b[j])
{flag=-1;break;}
if(flag==-2)
{flag=i+1;break;}
}
cout < <"a[]=" < <a < <endl;
cout < <"b[]=" < <b < <endl;
cout < <"flag=" < <flag < <endl;
}
不好意思,交个朋友吧
chaoxiaoshuai 2010-02-28
  • 打赏
  • 举报
回复
#include"iostream.h"
#include"string.h"
void mian()
{
char a[81],b[81];
int i=0,j,na,nb,flag;
cout<<"Input string a:"<<endl;
gets(a);
cout<<"Input string b:"<<endl;
gets(b);
na=strlen(a);nb=strlen(b);
flag=1;
for(i=0;na-i>=nb;i++)
{
flag=-2;
for(j=0;j<nb;j++)
if(a[i+j]!b[j])
{flag=-1;break;}
if(flag==-2)
{flag=i+1;break;}
}
cout<<"a[]="<<a<<endl;
cout<<"b[]="<<b<<endl;
cout<<"flag="<<flag<<endl;
}
stardust20 2010-02-28
  • 打赏
  • 举报
回复
看下这样可不可以
#include <iostream>
using namespace std;
int main()
{
string s="golden global view";
string r="global";
const char *show;

show=strstr(s.c_str(),r.c_str());//返回指向第一次出现r位置的指针,如果没找到则返回NULL。
cout<<show<<endl;
return 0;
}
开拓者Amadues 2010-02-28
  • 打赏
  • 举报
回复
有一个substring的函数你查查。

具体怎么用我不清楚,因为我是习惯用指针手动做这些事情的。
pengxincz 2010-02-28
  • 打赏
  • 举报
回复
在线等啊....急!!~~~~~~~~~~~

65,189

社区成员

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

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