當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C語言 strspn()用法及代碼示例


strspn()函數返回str1指向的字符串的初始子字符串的長度,該字符串僅由str2指向的字符串中包含的那些字符組成。

用法:

size_t strspn(const char *str1, const char *str2)
str1: string to be scanned.
str2: string containing the 
characters to match.
返回值: This function
returns the number of characters
in the initial segment of str1 
which consist only of characters 
from str2.
   
// C program to illustrate strspn() function 
#include <stdio.h> 
#include <string.h> 
  
int main () { 
   int len = strspn("geeks for geeks","geek"); 
   printf("Length of initial segment matching:%d\n", len );     
   return(0); 
}

輸出:


Length of initial segment matching 4
   
// C program to illustrate strspn() function 
#include <stdio.h> 
#include <string.h> 
  
int main () { 
   int len = strspn("i am","xyz"); 
   printf("Length of initial segment matching:%d\n", len ); 
   return(0); 
}

輸出:

Length of initial segment matching 0


相關用法


注:本文由純淨天空篩選整理自 strspn() function in C。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。