strcspn, wcscspn, _mbscspn
Returns the index of the first occurrence of a character in a string that belongs to a set of characters.
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 );
Parameters
- string
- Null-terminated searched string.
- strCharSet
- Null-terminated character set.
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.
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. The functions that convert to lowercase are affected by the current locale setting.
Generic-Text Routine Mappings
| TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
|---|---|---|---|
| _tcscspn | strcspn | _mbscspn | wcscspn |
Requirements
| Routine | Required header | Compatibility |
|---|---|---|
| strcspn | <string.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
| wcscspn | <string.h> or <wchar.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
| _mbscspn | <mbstring.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
// crt_strcspn.c
#include <string.h>
#include <stdio.h>
int 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
String Manipulation Routines | strncat | strncmp | strncpy | _strnicmp | strrchr | strspn | Run-Time Routines and .NET Framework Equivalents