strspn, wcsspn, _mbsspn, _mbsspn_l

Returns the index of the first character in a string that does not belong to a set of characters.


_mbsspn and _mbsspn_l cannot be used in applications that execute in the Windows Runtime. For more information, see CRT functions not supported with /ZW.

size_t strspn(
   const char *str,
   const char *strCharSet 
size_t wcsspn(
   const wchar_t *str,
   const wchar_t *strCharSet 
size_t _mbsspn(
   const unsigned char *str,
   const unsigned char *strCharSet 
size_t _mbsspn_l(
   const unsigned char *str,
   const unsigned char *strCharSet,
   _locale_t locale


  • str
    Null-terminated string to search.

  • strCharSet
    Null-terminated character set.

  • locale
    Locale to use.

Return Value

Returns an integer value specifying the length of the substring in str that consists entirely of characters in strCharSet*.* If str begins with a character not in strCharSet*,* the function returns 0.


The strspn function returns the index of the first character in str that does not belong to the set of characters in strCharSet. The search does not include terminating null characters.

wcsspn and _mbsspn are wide-character and multibyte-character versions of strspn**.** The arguments of wcsspn are wide-character strings; those of _mbsspn are multibyte-character strings. _mbsspn validates its parameters. If str or strCharSet isNULL, the invalid parameter handler is invoked, as described in Parameter Validation . If execution is allowed to continue, _mbspn sets errno to EINVAL and returns 0. strspn and wcsspn do not validate their parameters. These three functions behave identically otherwise.

The output value is affected by the setting of the LC_CTYPE category setting of the locale; see setlocale for more information. The versions of these functions without the _l suffix use the current locale for this locale-dependent behavior; the versions with the _l suffix are identical except that they use the locale parameter passed in instead. For more information, see Locale.

Generic-Text Routine Mappings

TCHAR.H routine

_UNICODE & _MBCS not defined

_MBCS defined

_UNICODE defined











Required header




<string.h> or <wchar.h>

_mbsspn, _mbsspn_l


For additional compatibility information, see Compatibility in the Introduction.


// crt_strspn.c
// This program uses strspn to determine
// the length of the segment in the string "cabbage"
// consisting of a's, b's, and c's. In other words,
// it finds the first non-abc letter.

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

int main( void )
   char string[] = "cabbage";
   int  result;
   result = strspn( string, "abc" );
   printf( "The portion of '%s' containing only a, b, or c "
           "is %d bytes long\n", string, result );
The portion of 'cabbage' containing only a, b, or c is 5 bytes long

.NET Framework Equivalent


See Also


String Manipulation (CRT)


Interpretation of Multibyte-Character Sequences

_strspnp, _wcsspnp, _mbsspnp, _mbsspnp_l

strcspn, wcscspn, _mbscspn, _mbscspn_l

strncat, _strncat_l, wcsncat, wcsncat_l, _mbsncat _mbsncat_l

strncmp, wcsncmp, _mbsncmp, _mbsncmp_l

strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l

_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l

strrchr, wcsrchr, _mbsrchr, _mbsrchr_l