函式
<cstring>

strcspn

size_t strcspn ( const char * str1, const char * str2 );
獲取字串中直到指定字元的跨度
掃描 str1,查詢 str2 中任意一個字元首次出現的位置,返回在此之前讀取的 str1 的字元數。

搜尋範圍包括終止的空字元。因此,如果在 str1 中沒有找到 str2 中的任何字元,該函式將返回 str1 的長度。

引數

str1
要被掃描的 C 字串。
str2
包含要匹配的字元的 C 字串。

返回值

str1 的初始部分中包含任何 str2 中字元的長度。
如果在 str1 中沒有找到 str2 中的任何字元,則返回 str1 的長度。
size_t 是一個無符號整數型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
/* strcspn example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "fcba73";
  char keys[] = "1234567890";
  int i;
  i = strcspn (str,keys);
  printf ("The first number in str is at position %d.\n",i+1);
  return 0;
}

輸出

The first number in str is at position 5


另見