strspn ()- 尋找第一個不相符字元的偏移

格式

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

語言層次

ANSI

安全執行緒

區分語言環境

此函數的行為可能受現行語言環境的 LC_CTYPE 種類影響。 如需相關資訊,請參閱 瞭解 CCSID 及語言環境

說明

strspn() 函數會尋找 string1 中第一個出現的字元,該字元未包含在 string2指定的字元集中。 在比對程序中不會考量 string2 結尾的空值字元 (\0)。

回覆值

strspn() 函數會傳回找到的第一個字元的索引。 此值等於 string1 的起始子字串長度,該子字串完全由 string2中的字元組成。 如果 string1 以不在 string2中的字元開頭,則 strspn() 函數會傳回 0。 如果在 string2中找到 string1 中的所有字元,則會傳回 string1 的長度。

範例

此範例會尋找陣列 字串 中第一個出現的字元,該字元不是 abc。 因為此範例中的字串是 cercle,所以 strspn() 函數會傳回 5,即 cercle 區段的長度,位於非 a、bc字元之前。
#include <stdio.h>
#include <string.h>
 
int main(void)
{
  char * string = "cabbage";
  char * source = "abc";
  int index;
 
  index = strspn( string, "abc" );
  printf( "The first %d characters of \"%s\" are found in \"%s\"\n",
          index, string, source );
}
 
/*****************  Output should be similar to:  *****************
 
The first 5 characters of "cabbage" are found in "abc"
*/

相關資訊