strspn, strcspn - search a string for a set of bytes
strspn,strcspn-在字符串中查找字节序列
所需头文件
#include <string.h>
size_t strspn(const char *s, const char *accept);
size_t strcspn(const char *s, const char *reject);
The strspn() function calculates the length (in bytes) of the initial segment of s which consists entirely of bytes in accept.
The strcspn() function calculates the length of the initial segment of s which consists entirely of bytes not in reject.
strspn函数计算从字符串s开始完全包含于accpet字符串的字符长度
运行结果如下:
strspn,strcspn-在字符串中查找字节序列
所需头文件
#include <string.h>
size_t strspn(const char *s, const char *accept);
size_t strcspn(const char *s, const char *reject);
The strspn() function calculates the length (in bytes) of the initial segment of s which consists entirely of bytes in accept.
The strcspn() function calculates the length of the initial segment of s which consists entirely of bytes not in reject.
strspn函数计算从字符串s开始完全包含于accpet字符串的字符长度
strcspn函数计算从字符串s开始完全不包含与accept字符串的字符长度
testcase如下:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *s1 = "123abcdefg";
const char *s2 = "ab";
const char *s3 = "12";
int tmp1 = strcspn(s1, s2);
int tmp2 = strspn(s1, s3);
printf("tmp1 = %d\n", tmp1);
printf("tmp2 = %d\n", tmp2);
return 0;
}
运行结果如下:
cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ ./a.out
tmp1 = 3
tmp2 = 2
可以看到从“123abcdefg”开始完全不包含于“ab”字符串的字符是123三个,所以tmp1的值是3
从“123abcdefg”开始完全包含于“12”字符串的字符是12两个,所以tmp2的值是2