检查字符串是否包含C 中的子字符串
我想知道是否有办法在char*
数组中查找substring
。
我似乎无法从Java
与C
相当的Contains()
例如:
char *buff = "this is a test string. The string contains random text.";
假设我输入substring
以与buff
进行比较,并查看在buff
找到substring
次数。
我能够找到第一次出现,但有多次出现的问题。 有人可以帮我吗?
有一个名为strstr
的标准C库函数可以进行子字符串搜索:
#include char *strstr(const char *haystack, const char *needle); The strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating null bytes ('\0') are not compared.
这样的函数名为strstr
并在头文件
声明
来自C标准
7.23.5.7 strstrfunction概要1
#include char *strstr(const char *s1, const char *s2);
说明2 strstr函数定位s2指向的字符串中字符序列(不包括终止空字符)的s1指向的字符串中的第一个匹配项。
返回3 strstr函数返回指向定位字符串的指针,如果找不到字符串则返回空指针。 如果s2指向长度为零的字符串,则该函数返回s1。