本篇博客旨在整理出所有Linux网络编程中常用的字符串相关的函数,这些函数普遍用在处理网络通信中的字符串数据。话不多说,直接进入正题。
1、strpbrk
函数
#include<string.h>
char* strpbrk(char* s1, char* s2);
strpbrk()
从第一个字符指针指向的位置 向后检查每个字符,直到遇到\0
(不检查\0
),如果检查到的某个字符在s2
指向的字符串中,那么返回他的地址,并停止检查。
如果s1
、s2
没有相同字符,返回NULL
。
注:传入strpbrk
函数的两个变量可以是一个指针,也可以是一个字符串。
2、strcasecmp
函数
#include<string.h>
int strcasecmp(const char* s1, const char* s2);
忽略大小写地按字典序比较s1
、s2
两个字符串。
返回值类型是int
,如果s1
、s2
相等,则返回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
函数的含义一样,只不过这个函数忽略大小写地找。