傳回字串中屬於字元集之第一個出現的字元索引。
重要
在 Windows 執行階段中執行的應用程式中無法使用 _mbschr 和 _mbschr_l。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 Windows 平台應用程式中不支援的 CRT 函式)。
語法
size_t strcspn(
const char *str,
const char *strCharSet
);
size_t wcscspn(
const wchar_t *str,
const wchar_t *strCharSet
);
size_t _mbscspn(
const unsigned char *str,
const unsigned char *strCharSet
);
size_t _mbscspn_l(
const unsigned char *str,
const unsigned char *strCharSet,
_locale_t locale
);
參數
str
以 Null 結束的搜尋字串。
strCharSet
以 Null 結束的字元集。
locale
要使用的地區設定。
傳回值
這些函式會傳回 str 中位於 strCharSet 的第一個字元索引。 如果 str 中沒有位於 strCharSet 的字元,則傳回值會是 str 的長度。
未保留表示錯誤的傳回值。
備註
wcscspn 和 _mbscspn 分別是 strcspn 的寬字元版本和多位元組字元版本。 的自變數 wcscspn 是寬字元字串。 的自變數和傳回值 _mbscspn 是多位元組位元元字串。
_mbscspn 會驗證其參數。 str如果 或 strCharSet 為 Null 指標,則會叫用無效的參數處理程式,如參數驗證中所述。 如果允許繼續執行,則函式會傳回 0 並將 errno 設定為 EINVAL。 strcspn 和 wcscspn 不會驗證其參數。 除此之外,這三個函式的行為相同。
輸出值會受到設定地區設定之 LC_CTYPE 類別設定的影響。 如需詳細資訊,請參閱setlocale。 這些沒有 _l 後置字元的函式版本,會針對此與地區設定相關的行為使用目前的地區設定;具有 _l 後置字元的版本也一樣,只不過它們會改用傳遞的地區設定參數。 如需詳細資訊,請參閱 Locale。
根據預設,此函式的全域狀態會限定於應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。
一般文字常式對應
| TCHAR.H 常式 | _UNICODE 和 _MBCS 未定義 |
_MBCS 已定義 |
_UNICODE 已定義 |
|---|---|---|---|
_tcscspn |
strcspn |
_mbscspn |
wcscspn |
需求
| 常式 | 必要的標頭 |
|---|---|
strcspn |
<string.h> |
wcscspn |
<string.h> 或 <wchar.h> |
_mbscspn, _mbscspn_l |
<mbstring.h> |
如需相容性詳細資訊,請參閱相容性。
範例
// crt_strcspn.c
#include <string.h>
#include <stdio.h>
void test( const char * str, const char * strCharSet )
{
int pos = strcspn( str, strCharSet );
printf( "strcspn( \"%s\", \"%s\" ) = %d\n", str, strCharSet, pos );
}
int main( void )
{
test( "xyzbxz", "abc" );
test( "xyzbxz", "xyz" );
test( "xyzbxz", "no match" );
test( "xyzbxz", "" );
test( "", "abc" );
test( "", "" );
}
strcspn( "xyzbxz", "abc" ) = 3
strcspn( "xyzbxz", "xyz" ) = 0
strcspn( "xyzbxz", "no match" ) = 6
strcspn( "xyzbxz", "" ) = 6
strcspn( "", "abc" ) = 0
strcspn( "", "" ) = 0
另請參閱
字串操作
地區設定
多位元組字元序列的解譯
strncat、、_strncat_lwcsncat、_wcsncat_l、、_mbsncat、_mbsncat_l
strncmp、 、 wcsncmp、 _mbsncmp_mbsncmp_l
strncpy、、_strncpy_lwcsncpy、_wcsncpy_l、、_mbsncpy、_mbsncpy_l
_strnicmp、、_wcsnicmp_mbsnicmp、_strnicmp_l、、_wcsnicmp_l、_mbsnicmp_l
strrchr、 、 wcsrchr、 _mbsrchr_mbsrchr_l
strspn、 、 wcsspn、 _mbsspn_mbsspn_l