C++ cstring strspn() 函数

定义和用法

strspn() 函数用于搜索 C 风格字符串中第一个不匹配指定字符集中任意字符的字符,并返回从字符串开头到该字符之前的子字符串长度。

strspn() 函数定义在 <cstring> 头文件中。

实例

测量字符串中到第一个非数字字符为止的长度:

char myStr[] = "4096 bytes should be enough";
int pos = strspn(myStr, "0123456789");
cout << pos;

亲自试一试

语法

strspn(void * str, void * search);

参数

参数 描述
str 必需。要搜索的字符串。
search 必需。包含要搜索的字符集的字符串。

技术细节

返回: 整数,表示第一个不匹配搜索字符集中任意字符的位置索引。