c++ 判断char* 中是否含有string中的字符串
时间: 2023-05-23 21:01:00 浏览: 62
可以使用标准库中的strstr函数,该函数会在char*字符串中搜索是否含有另一个字符串string,如果有则返回该字符串首次出现的位置,如果没有则返回NULL。以下是一个示例代码:
```C
#include <stdio.h>
#include <string.h>
int main() {
char* str = "This is a string";
char* substr1 = "is";
char* substr2 = "not";
if (strstr(str, substr1) != NULL) {
printf("'%s' is found in '%s'\n", substr1, str);
} else {
printf("'%s' is not found in '%s'\n", substr1, str);
}
if (strstr(str, substr2) != NULL) {
printf("'%s' is found in '%s'\n", substr2, str);
} else {
printf("'%s' is not found in '%s'\n", substr2, str);
}
return 0;
}
```
输出结果为:
```
'is' is found in 'This is a string'
'not' is not found in 'This is a string'
```