C 庫函數 - strcspn()

C 標準庫 - <string.h> C 標準庫 - <string.h>

描述

C 庫函數 size_t strcspn(const char *str1, const char *str2) 檢索字串 str1 開頭連續有幾個字元都不含字串 str2 中的字元。

聲明

下麵是 strcspn() 函數的聲明。

size_t strcspn(const char *str1, const char *str2)

參數

  • str1 -- 要被檢索的 C 字串。
  • str2 -- 該字串包含了要在 str1 中進行匹配的字元列表。

返回值

該函數返回 str1 開頭連續都不含字串 str2 中字元的字元數。

實例

下麵的實例演示了 strcspn() 函數的用法。

#include <stdio.h>
#include <string.h>


int main ()
{
   int len;
   const char str1[] = "ABCDEF4960910";
   const char str2[] = "013";

   len = strcspn(str1, str2);

   printf("第一個匹配的字元是在 %d\n", len + 1);

   return(0);
}

讓我們編譯並運行上面的程式,這將產生以下結果:

第一個匹配的字元是在 10

C 標準庫 - <string.h> C 標準庫 - <string.h>