and
div
eof
not
or
xor
Expand Minimize
This topic has not yet been rated - Rate this topic

strcspn, wcscspn, _mbscspn, _mbscspn_l

Returns the index of the first occurrence in a string, of a character that belongs to a set of characters.

Important note Important

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

size_t strcspn(
   const char *str,
   const char *strCharSet 
);
size_t wcscspn(
   const wchar_t *str,
   const wchar_t *strCharSet 
);
size_t _mbscspn(
   const unsigned char *str,
   const unsigned char *strCharSet 
);
size_t _mbscspn_l(
   const unsigned char *str,
   const unsigned char *strCharSet,
   _locale_t locale
);
str

Null-terminated searched string.

strCharSet

Null-terminated character set.

locale

Locale to use.

These functions return the index of the first character in str that is in strCharSet. If none of the characters in str is in strCharSet, then the return value is the length of str.

No return value is reserved to indicate an error.

wcscspn and _mbscspn are wide-character and multibyte-character versions of strcspn. The arguments of wcscspn are wide-character strings; those of _mbscspn are multibyte-character strings.

_mbscspn validates its parameters. If either str or strCharSet is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, the function returns 0 and sets errno to EINVAL. strcspn and wcscspn 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

_tcscspn

strcspn

_mbscspn

wcscspn

n/a

n/a

_mbscspn_l

n/a

Routine

Required header

strcspn

<string.h>

wcscspn

<string.h> or <wchar.h>

_mbscspn , _mbscspn_l

<mbstring.h>

For additional compatibility information, see Compatibility.

// crt_strcspn.c

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

void test( const char * str, const char * strCharSet )
{
   int pos = strcspn( str, strCharSet );
   printf( "strcspn( \"%s\", \"%s\" ) = %d\n", str, strCharSet, pos );    
}

int main( void )
{
   test( "xyzbxz", "abc" );
   test( "xyzbxz", "xyz" );
   test( "xyzbxz", "no match" );
   test( "xyzbxz", "" );
   test( "", "abc" );
   test( "", "" );
}
strcspn( "xyzbxz", "abc" ) = 3
strcspn( "xyzbxz", "xyz" ) = 0
strcspn( "xyzbxz", "no match" ) = 6
strcspn( "xyzbxz", "" ) = 6
strcspn( "", "abc" ) = 0
strcspn( "", "" ) = 0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.