字符串函数
字符串函数包含在 string.h 这个头文件中
- strlen():返回不包括字符串结尾’\0’的字符串长度,即有效字符长度
int strlen(const char* str); // 函数原型
int a = strlen(str); //函数使用案例
- strnlen():返回len个char长度以内,不包括字符串结尾’\0’的字符串长度
int strnlen(const char* str, int maxlen); //函数原型
int a = strlen(str, len); //函数使用案例
- strcat():将参数str2追加到str1里
int strcat(char* str1, const char* str2); // 函数原型
strcat(str1, str2); // 函数使用案例
- strncat():字符串有限追加len个char字节长度的字符串
int strncat(str1, str2, len); // 函数原型
strncat(str1, str2, len); //函数使用案例
- strtok():分解字符串str为一组字符串子串,石头人就是要被分隔的支付穿,delim为分隔符
char* strtok(char* str, const char* str2, int len); // 函数原型
strtok(str, delim); // 函数案例
strtok()函数每次会把分隔符坐在的位置置为’\0’,调用前和调用后的str已经不一样了
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[20] = "abcsabscccs";
strtok(s, "s");
printf("%s\n", s);
strtok(NULL, "s");
printf("%s\n", s);
strtok(NULL, "s");
printf("%s\n", s);
}
案例输出结果为
abc
abc
abc