Linux 中与字符串相关的函数strpbrk、strcasecmp、strspn(不间断更新)

2 篇文章 0 订阅

本篇博客旨在整理出所有Linux网络编程中常用的字符串相关的函数,这些函数普遍用在处理网络通信中的字符串数据。话不多说,直接进入正题。

1、strpbrk函数

#include<string.h>
char* strpbrk(char* s1, char* s2);

strpbrk()从第一个字符指针指向的位置 向后检查每个字符,直到遇到\0(不检查\0),如果检查到的某个字符在s2指向的字符串中,那么返回他的地址,并停止检查。

如果s1s2没有相同字符,返回NULL

注:传入strpbrk函数的两个变量可以是一个指针,也可以是一个字符串。

2、strcasecmp函数

#include<string.h>
int strcasecmp(const char* s1, const char* s2);

忽略大小写按字典序比较s1s2两个字符串。
返回值类型是int,如果s1s2相等,则返回0,如果是s1大,那么返回s1中第一个大于s2的字符的int型 正数ASCII 码,反之,返回s2中第一个大于s1的字符的int型 负数ASCII 码。

实例:

#include<stdio.h>
#include<string.h>
#include<iostream>

using namespace std;

int main()
{
	char a[30] = {"asdasd"};
	char b[30] = {"asdasdasda"};
	char* p;
	p = b;
	auto x = strcasecmp(p,a);
	printf("%d\n",x);
}

输出:

97
#include<stdio.h>
#include<string.h>
#include<iostream>

using namespace std;

int main()
{
	char a[30] = {"asdasd"};
	char b[30] = {"asdasd"};
	char* p;
	p = b;
	auto x = strcasecmp(p,a);
	printf("%d\n", x);
}

输出:

0
#include<stdio.h>
#include<string.h>
#include<iostream>

using namespace std;

int main()
{
	char a[30] = {"asdasd"};
	char b[30] = {"asdasdasda"};
	char* p;
	p = b;
	auto x = strcasecmp(p,a);
	printf("%d\n",x);
}

输出:

97

3、strncasecmp函数

#include<string.h>
int strncasecmp(const char* s1, const char* s2, size_t n);

与上面函数的区别:只比较前n个字符

4、strspn函数

#include<string.h>
size_t strspn(const char* str, const char* accept);

计算字符串 str 中连续有几个字符属于字符串 accept,返回计算结果。
注意:

  • 找的不是子串,也就是:对字符的顺序没有要求,只要这个字符在accept里面出现过就行
  • 虽然对顺序没有要求,但是,这些字符必须连续,只要中间被一个accept没有的字符断开了,那么后面的就不算了,只算前面的。
  • str的第一个字符开始计数,只要你str的第一个字符在accept里面都没出现过,那就不往下算了,直接返回0

实例:

#include<stdio.h>
#include<string.h>
#include<iostream>

using namespace std;

int main()
{
	char a[30] = { "asdasd"};
	char b[30] = { "asdasd"};
	char* p;
	p = b;
	auto x = strspn(p, a);		正好完全匹配
	printf( "%ld\n", x);return 0;	
}

输出:

6
#include<stdio.h>
#include<string.h>
#include<iostream>

using namespace std;

int main()
{
	char a[30] = { "asdasd"};
	char b[30] = { "wasdasd"};
	char* p;
	p = b;
	auto x = strspn(p, a);		第一个 w 就不匹配了
	printf( "%ld\n", x);return 0;
}

输出:

0
#include<stdio.h>
#include<string.h>
#include<iostream>

using namespace std;

int main()
{
	char a[30] = { "asdasdw"};
	char b[30] = { "wasdasd"};
	char* p;
	p = b;
	auto x = strspn(p, a);		b 里的字符在 a 里面都能找到,与出现顺序无关
	printf( "%ld\n", x);return 0;
}

输出:

7
#include<stdio.h>
#include<string.h>
#include<iostream>

using namespace std;

int main()
{
	char a[30] = { "asdasd"};
	char b[30] = { "asdwasd"};
	char* p;
	p = b;
	auto x = strspn(p, a);		被 w 断开了
	printf( "%ld\n", x);return 0;
}

输出:

3
#include<stdio.h>
#include<string.h>
#include<iostream>

using namespace std;

int main()
{
	char a[30] = { "wasdasd"};
	char b[30] = { "asdwasd"};
	char* p;
	p = b;
	auto x = strspn(p, a);
	printf( "%ld\n", x);return 0;
}

输出:

7

5、strchr函数

#include<string.g>
char* strchr(const char* str, int c);

str指向的字符串中寻找第一次出现字符c的位置,并返回指向该位置的指针,如果没找到则返回NULL。

注意:c的类型是int,但是在使用该函数时既可以传入一个字符(char类型的),也可以传入一个int型的ASCII码

6、strncpy函数

#include<string.g>
char* strncpy(char* dest, const char* src, size_t n);

这是一个拷贝字符串的函数。他将src串中前n个字符依次拷贝到dest所指向的位置。

7、strstr函数

#include<string.h>
char* strstr(const char* havstack, const char* needle);

找到子串needle在 主串havstack中第一次出现的位置,返回的是指向该位置的指针。
返回值:找不到返回NULL

8、strcasestr函数

char* strcasestr(const char* havstack, const char* needle);

strstr函数的含义一样,只不过这个函数忽略大小写地找。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值