检查字符串是否在C中包含子字符串 [英] Checking if string contains substring in C

查看:179
本文介绍了检查字符串是否在C中包含子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在 char * 数组中查找子字符串

I was wondering if there is a way to look for a substring in a char* array.

我似乎无法从 Java Contains() c $ c>表示 C

I can't seem to find an equivalent of Contains() from Java for C

例如:

char * buff =这是一个测试字符串。该字符串包含随机文本。;

假定我输入了子字符串来与 buff 进行比较,看看子字符串 buff 中找到。

suppose I input the substring to compare to buff and see how many times is substring found in buff.

我能够找到第一个匹配项,但是有多个匹配项的问题。有人可以帮我吗?

I am able to find the first occurrence but having issues with multiple occurrences. Can someone assist me?

推荐答案

有一个标准的C库函数,称为 strstr 进行子字符串搜索:

There is a standard C library function called strstr which does substring searches:

#include <string.h>

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.

这篇关于检查字符串是否在C中包含子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆