strtok,strspn、strcspn和strpbrk
来源:互联网 发布:阿里云 rds sql审计 编辑:程序博客网 时间:2024/06/15 15:18
<string.h>
Finds the next token in a null-terminated byte string pointed to by str
. The separator characters are identified by null-terminated byte string pointed to by delim
.
This function is designed to be called multiples times to obtain successive tokens from the same string.
- If str != NULL, the call is treated as the first call to
strtok
for this particular string. The function searches for the first character which is not contained indelim
.
- If no such character was found, there are no tokens in
str
at all, and the function returns a null pointer. - If such character was found, it is the beginning of the token. The function then searches from that point on for the first character that is contained in
delim
.
- If no such character was found,
str
has only one token, and future calls tostrtok
will return a null pointer - If such character was found, it is replaced by the null character '\0' and the pointer to the following character is stored in a static location for subsequent invocations.
- If no such character was found,
- The function then returns the pointer to the beginning of the token
- If no such character was found, there are no tokens in
- If str == NULL, the call is treated as a subsequent calls to
strtok
: the function continues from where it left in previous invocation. The behavior is the same as if the previously stored pointer is passed as str.
Parameters
str-pointer to the null-terminated byte string to tokenizedelim-pointer to the null-terminated byte string identifying delimitersReturn value
Pointer to the beginning of the next token or NULL if there are no more tokens.
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdlib.h>int main(void){ char *str = (char*)malloc(20); char *tok = NULL; int len = 0; strcpy(str, "This is a string"); len = strlen(str); printf("string before strtok(): %s\n", str); tok = strtok(str, " "); while (tok) { printf("Token: %s\n", tok); tok = strtok(NULL, " "); } printf("Print mangled string after strtok()\n"); for (int i = 0; i < len; i++) { if (str[i] == '\0') { printf("'\\0'"); } else { printf("%c", str[i]); } } printf("\n"); free(str); return 0;}
输出为
string before strtok(): This is a string
Token: This
Token: is
Token: a
Token: string
Print mangled string after strtok()
This'\0'is'\0'a'\0'string
<string.h>
Returns the length of the maximum initial segment of the byte string pointed to by dest
, that consists of only the characters found in byte string pointed to by src
.
Parameters
dest-pointer to the null-terminated byte string to be analyzedsrc-pointer to the null-terminated byte string that contains the characters to search forReturn value
The length of the maximum initial segment that contains only characters from byte string pointed to by src
strspn返回dest中包含src连续段最长的个数
#include <string.h>#include <stdio.h> int main(void){ char *string_find = "abcde312$#@"; char *low_alpha = "qwertyuiopasdfghjklzxcvbnm"; printf("%zu", strspn(string_find, low_alpha)); return 0;}
输出为
5
<string.h>
Scans the null-terminated byte string pointed to by dest
for any character from the null-terminated byte string pointed to by breakset
, and returns a pointer to that character.
Parameters
dest-pointer to the null-terminated byte string to be analyzedbreakset-pointer to the null-terminated byte string that contains the characters to search forReturn value
Pointer to the first character in dest
, that is also in breakset
, or null pointer if no such character exists.
#include <stdio.h>#include <string.h> int main(void){ const char* str = "hello world, friend of mine!"; const char* sep = " ,!"; unsigned int cnt = 0; do { str = strpbrk(str, sep); // find separator if(str) str += strspn(str, sep); // skip separator ++cnt; // increment word count } while(str && *str); printf("There are %d words\n", cnt);}
输出为
There are 5 words
<string.h>
Returns the length of the maximum initial segment of the byte string pointed to by dest
, that consists of only the characters not found in byte string pointed to by src
.
Parameters
dest-pointer to the null-terminated byte string to be analyzedsrc-pointer to the null-terminated byte string that contains the characters to search forReturn value
The length of the maximum initial segment that contains only characters not found in the byte string pointed to by src
Example
#include <string.h>#include <stdio.h> int main(void){ char *string_find = "abcdefg**"; char *character = "*"; printf("%zu", strcspn(string_find, character)); return 0;}
Output:
7
- strtok,strspn、strcspn和strpbrk
- 字符串函数strspn、strcspn和strpbrk
- strpbrk strspn strcspn
- C语言str系列库函数之strspn()、strcspn()和strpbrk()
- strspn和strcspn妙用
- strspn和strcspn妙用
- strspn和strcspn
- strspn 和strcspn
- strcspn和strspn函数
- strcspn和strspn函数
- strspn 和strcspn
- strspn 和strcspn
- strspn 和strcspn
- strspn 和strcspn
- strpbrk和strcspn用法
- http解析中的一些东西,strpbrk,strcasecmp,strncasecmp,strspn,strcspn
- string库之模拟实现strspn、strcspn与strpbrk
- strspn和strcspn方法学习
- SSH通过代码导入xls文件
- Node.js中的事件驱动编程详解
- 剑指offer 实现数组的全排列
- apache2.4+php5+mysql配置
- [toj1003]Transportation
- strtok,strspn、strcspn和strpbrk
- 【学习ios之路:C语言】函数及递归的简单应用
- hdu2652——Warching TV
- C++11初窥二: 继承构造函数和委派构造函数
- 序列的性能问题
- 客户端 用不用 bind 的区别
- ext
- Python IDE从pydev到PyCharm
- 64位Ubuntu Kylin14下编译安卓2.3.3过程记录