Find a substring in a string.
size_t strcspn( const char *string, const char *strCharSet );
size_t wcscspn( const wchar_t *string, const wchar_t *strCharSet );
size_t _mbscspn( const unsigned char *string, const unsigned char *strCharSet );
Routine | Required Header | Compatibility |
strcspn | <string.h> | ANSI, Win 95, Win NT |
wcscspn | <string.h> or <wchar.h> | ANSI, Win 95, Win NT |
_mbscspn | <mbstring.h> | Win 95, Win NT |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB | Single thread static library, retail version |
LIBCMT.LIB | Multithread static library, retail version |
MSVCRT.LIB | Import library for MSVCRT.DLL, retail version |
Return Value
Each of these functions returns an integer value specifying the length of the initial segment of string that consists entirely of characters not in strCharSet. If string begins with a character that is in strCharSet, the function returns 0. No return value is reserved to indicate an error.
Parameters
string
Null-terminated searched string
strCharSet
Null-terminated character set
Remarks
The strcspn function returns the index of the first occurrence of a character in string that belongs to the set of characters in strCharSet. Terminating null characters are included in the search.
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. These three functions behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine | _UNICODE & _MBCS Not Defined | _MBCS Defined | _UNICODE Defined |
_tcscspn | strcspn | _mbscspn | wcscspn |
Example
/* STRCSPN.C */
#include <string.h>
#include <stdio.h>
void main( void )
{
char string[] = "xyzabc";
int pos;
pos = strcspn( string, "abc" );
printf( "First a, b or c in %s is at character %d\n",
string, pos );
}
Output
First a, b or c in xyzabc is at character 3
See Also strncat, strncmp, strncpy, _strnicmp, strrchr, strspn