C++ 在字符数组中搜索子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20649390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:15:57 来源:igfitidea点击:
searching substrings in char array
提问by user3113716
As I know there is a function in C++ string which performs substring search: string1.find(string2).
据我所知,C++ 字符串中有一个函数可以执行子字符串搜索:string1.find(string2)。
Is there any way to perform the same action in char? Below is my question:
有没有办法在char中执行相同的操作?下面是我的问题:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char a[]="hello world!";
char b[]="el";
//find substring b[] in a[]
}
let's say i need to find substring "el" in the string, is it possible to do that?
假设我需要在字符串中找到子字符串“el”,可以这样做吗?
回答by Elixir Techne
char *output = NULL;
output = strstr (a,b);
if(output) {
printf("String Found");
}
回答by Rohit Jose
This should work:
这应该有效:
char a[]="hello world!";
char b[]="el";
//find substring b[] in a[]
string str(a);
string substr(b);
found = str.find(substr);