字符串相关函数整理

string.h 中字符串相关函数
按功能分类:

字符串连接:
strcat : char* strcat(char* destination, const char* source);

strncat : char* strcat(char* destination, const char* source, size_t num);

cat 意思是catenate。连接的意思。

strncp 是strcat的安全版本号。能够防止越界。

从定义上能够看出带是将const的字符串连接到不带const的字符串上。

字符/字符串查找:
strchr : char* strchr(const char* str, int character); char* strchr(char* str, int character);

查找字符character在str中第一次出现的位置。返回一个指针指向该字符到字符串结尾。



因为'\0'也是字符串的一部分,故该函数也能够查找结束符。

chr意思应该是character。

strrchr : char* strchr(const char* str, int character); char* strchr(char* str, int character);

最后一次出现的位置。

'r'应该是right的意思。也就是从右開始搜索。

strpbrk : const char* strpbrk(const char* str1,const char* str2); char* strpbrk(char* str1,const char* str2);

查找str2中随意字符在str1中第一次出现的位置,返回从整个位置到str1最后的字符串。

不包括结束符。

strcspn : size_t strcspn(const char* str1, const char* str2);

返回的是str1第一次出现str2中随意字符的位置。搜索包括null结束符,所以函数能够返回str1字符串的长度当没有不论什么匹配的时候。



strstr : const char* strstr(const char* str1, const char* str2); char* strstr(char* str1, const char* str2);

匹配完整字符串

字符串比較:
strcmp: int strcmp(const char* str1, const char* str2);

strncmp: int strcmp(const char* str1, const char* str2, size_t num);

strcoll: int strcoll(const char* str1, const char* str2);

依据LC_COLIATE比較str1和str2。

字符串拷贝:
strcpy : char* strcpy(char* destination, const char* source);

strncpy : char* strcpy(char* destination, const char* source, size_t num);

拷贝source到destination中去。遇到'\0'停止copy。

获取字符串长度:
strlen: size_t strlen(const char* str);

拆分字符串:
strtok: char* strtok(char* str, const char* delimiters);

tok的意思是token。该函数就是将str依据delimiter拆分。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值