检查字符串是否在C中包含子字符串

编程入门 行业动态 更新时间:2024-10-15 00:27:41
本文介绍了检查字符串是否在C中包含子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

我似乎无法从 Java 表示 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中包含子字符串

本文发布于:2023-11-29 16:08:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646895.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!