strspn, wcsspn, _mbsspn, _mbsspn_l

返回字符串中不属于指定字符集的第一个字符的索引。

重要

_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 会验证其参数。 如果 strstrCharSetNULL,则将调用无效参数处理程序,如参数验证中所述。 如果允许执行继续,则 _mbspnerrno 设置为 EINVAL,并返回 0。 strspnwcsspn 不会验证其参数。 否则这三个函数否则具有相同行为。

输出值受区域设置的 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>
_mbsspn_mbsspn_l <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

另请参阅

字符串操作
区域设置
多字节字符序列的解释
_strspnp_wcsspnp_mbsspnp_mbsspnp_l
strcspnwcscspn_mbscspn_mbscspn_l
strncat_strncat_lwcsncat_wcsncat_l_mbsncat_mbsncat_l
strncmpwcsncmp_mbsncmp_mbsncmp_l
strncpy_strncpy_lwcsncpy_wcsncpy_l_mbsncpy_mbsncpy_l
_strnicmp_wcsnicmp_mbsnicmp_strnicmp_l_wcsnicmp_l_mbsnicmp_l
strrchrwcsrchr_mbsrchr_mbsrchr_l