strcspn ()- 查找第一个字符匹配的偏移

格式

#include <string.h>
size_t strcspn(const char *string1, const char *string2);

语言级别

ANSI

线程安全

语言环境敏感

此函数的行为可能受当前语言环境的 LC_CTYPE 类别影响。 有关更多信息,请参阅 了解 CCSID 和语言环境

描述

strcspn() 函数在 string1 中首次出现属于 string2指定的字符集的字符。 搜索中不考虑空字符。

strcspn() 函数对以 null 结束的字符串进行操作。 函数的字符串自变量应包含标记字符串结束的空字符 (\0)。

返回值

strcspn() 函数返回找到的第一个字符的索引。 此值相当于 string1 的初始子串的长度,该子串完全由不在 string2中的字符组成。

示例

此示例使用 strcspn()string中查找首次出现的任何字符 axle
#include <stdio.h>
#include <string.h>
 
#define SIZE 40
 
int main(void)
{
  char string[SIZE] = "This is the source string";
  char * substring = "axle";
 
  printf( "The first %i characters in the string \"%s\" "
          "are not in the string \"%s\" \n",
            strcspn(string, substring), string, substring);
 
}
 
/**********  Output should be similar to:  **************
 
The first 10 characters in the string "This is the source string"
are not in the string "axle"
*/

相关信息