返回字符串中不属于指定字符集的第一个字符的索引。
重要
_mbsspn 和 _mbsspn_l 无法用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数。
语法
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 终止的字符串。
strCharSet
null 终止的字符集。
locale
要使用的区域设置。
返回值
返回一个指定 str 中 substring 长度的整数值,该字符串包含 strCharSet 中的所有字符。 如果 str 以不在 strCharSet 中的字符开头,则该函数返回 0。
备注
strspn 函数返回 str 中不属于 strCharSet 字符集的第一个字符的索引。 搜索不包括终止 null 字符。
wcsspn 和 _mbsspn 分别是 strspn 的宽字符及多字节字符版本。 wcsspn 的自变量是宽字符字符串。 _mbsspn 的自变量是多字节字符字符串。 _mbsspn 会验证其参数。 如果 str 或 strCharSet 是 NULL,会调用无效参数处理程序,如参数验证中所述。 如果允许执行继续,则 _mbspn 将 errno 设置为 EINVAL,并返回 0。 strspn 和 wcsspn 不会验证其参数。 否则这三个函数否则具有相同行为。
输出值受区域设置的 LC_CTYPE 类别设置的影响。 有关详细信息,请参阅 setlocale。 这些不带 _l 后缀的函数的版本使用为该区域设置相关的行为的当前区域设置;带有 _l 后缀的版本相同,只不过它们使用传递的区域设置参数。 有关详细信息,请参阅 Locale。
默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态。
一般文本例程映射
| TCHAR.H 例程 | _UNICODE 和 _MBCS 未定义 |
_MBCS 已定义 |
_UNICODE 已定义 |
|---|---|---|---|
_tcsspn |
strspn |
_mbsspn |
wcsspn |
| 不适用 | 不适用 | _mbsspn_l |
不适用 |
要求
| 例程 | 必需的标头 |
|---|---|
strspn |
<string.h> |
wcsspn |
<string.h> 或 <wchar.h> |
| %> | <mbstring.h> |
有关兼容性的详细信息,请参阅 兼容性。
示例
// 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