C 语言 库 函数 strcspn 用于检索源 字符串 开头连续有几个字符都不含目的字符串中的 字符。
size_t strcspn(const char *str1, const char *str2);
参数 | 描述 |
---|---|
str1 | 要被检索的 C 字符串。 |
str2 | 该字符串包含了要在 str1 中进行匹配的字符列表。 |
该函数返回 str1 开头连续都不含字符串 str2 中字符的字符数。
使用 strcspn 函数,需要引入 string.h 头文件。
使用 strcspn() 函数,实现在字符串中匹配字符
#include <stdio.h>
#include <string.h>
int main()
{
printf("嗨客网(www.haicoder.net)\n\n");
char str[30] = "Hello,HaiCoder";
char subStr[10] = "llo";
int cnt = strcspn(str, subStr);
printf("retCnt = %d\n", cnt);
return 0;
}
程序运行后,控制台输出如下:
我们使用了 strcspn 函数,实现了在字符数组 str 中,开头连续有几个字符都不含目的字符串中的 subStr 中。
C 语言库函数 strcspn 用于检索源字符串开头连续有几个字符都不含目的字符串中的字符。