strspn, wcsspn, _mbsspn, _mbsspn_l

傳回字串中不屬於指定字元集之第一個字元的索引。

重要

在 Windows 執行階段中執行的應用程式中無法使用 _mbsspn_mbsspn_l。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 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 由 字元組成的 strCharSet 子字串長度。 如果 str 開頭為不在 中的 strCharSet 字元,則函式會傳回 0。

備註

strspn 式會傳回 中不屬於 中 strstrCharSet 字元集之第一個字元的索引。 搜尋不包含終止 Null 字元。

wcsspn_mbsspn 分別是 strspn 的寬字元版本和多位元組字元版本。 的引數 wcsspn 是寬字元字串。 的引數 _mbsspn 是多位元組字元字串。 _mbsspn 會驗證其參數。 如果 strstrCharSetNULL ,則會叫用不正確參數處理常式,如參數驗證 中所述 。 若允許繼續執行,_mbspn 會將 errno 設為 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
strcspn, wcscspn, _mbscspn, _mbscspn_l
strncat, _strncat_l, wcsncat, _wcsncat_l, _mbsncat, _mbsncat_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l