strspn() 函数原型如下:
[Plain Text] 纯文本查看 复制代码 #include <string.h>
size_t strspn(const char *s, const char *accept);
该函数查看字符串 s 一共连续有几个字符是被包含在 accept 这个字符串中的,函数返回的是这些个字符的个数。
测试代码:
[C++] 纯文本查看 复制代码 #include <stdio.h>
#include <string.h>
int main()
{
char *str = "hello linux world";
printf("%d\n", strspn(str, "hello"));
printf("%d\n", strspn(str, "zyxwvutsrqponmlkjhgfedcba")); /* str 中有空格,accept 在检测时遇到空格停止*/
printf("%d\n", strspn(str, "abcdefghjklmnopqrstuvwxyz ")); /* 这里的 accept 少了一个字符 'i' */
printf("%d\n", strspn(str, "abcdefghijklmnopqrstuvwxyz ")); /* str 中的字符在 accept 里都能找得到*/
return 0;
}
运行输出: |