C,C++中常用的操作字符串的函数
小小的做一个总结
函数名:stpcpy
功能:拷贝一个字符串到另一个,遇到'\0'时停止拷贝,destin要有足够大的空间
用法:char*stpcpy(char*destin,char*source);
函数名:strcat
功能:字符串拼接函数,注意,destin要有足够大的空间
用法:char*strcat(char*destin,char*source);
函数名:strchr
功能:在一个串中查找给定字符的第一个匹配之处,找不到返回NULL
用法:char*strchr(char*str,charc);
函数名:strcmp
功能: 字符串大小比较,str1>str2返回1,str1
函数名:strncmpi
功能:比较字符串str1和str2的前maxlen个字符,忽略大小写
用法:intstrncmpi(char*str1,char*str2,unsignedmaxlen);
函数名:strcpy
功能:遇到'\0'时停止拷贝,destin要有足够大的空间
用法:char*strcpy(char*str1,char*str2);
函数名:strcspn
功能:返回字符串s1中第一个在s2中出现的字符在s1中的下标值,亦即在s1中出现而s2中没有出现的子串的长度
用法:intstrcspn(char*str1,char*str2);
程序例:
str1="GoldenGlobalView";str2="new"
在s1中,且在s2中,ne w这三个字符都满足,而e是在s1中最先出现的,所以返回它的位置
函数名:strdup
功能:将串拷贝到新建的位置处,注意,在使用该函数时,申请了一片新的内存,所以使用后必须释放
用法:char*strdup(char*str);
程序例:
#include#include #include intmain(void) { char*dup_str,*string="abcde"; dup_str=strdup(string); //申请了一片新的内存地址,dup_str指向了这片内存 printf("%s\n",dup_str); free(dup_str); //一定要释放 return0; }
函数名:stricmp
功能:比较字符串大小,忽略大小写
用法:intstricmp(char*str1,char*str2);
函数名:strerror
功能:返回指向错误信息字符串的指针
用法:char*strerror(interrnum);
程序例:
#include#include intmain(void) { char*buffer; buffer=strerror(errno); printf("Error:%s\n",buffer); return0; }
函数名:strcmpi
功能:比较两个字符串,忽略大小写
用法:intstrcmpi(char*str1,char*str2);
函数名:strncmp
功能:比较字符串大小 maxlen是比较的位数
用法:intstrncmp(char*str1,char*str2,intmaxlen);
函数名:strncmpi
功能:把串中的一部分与另一串中的一部分比较,不管大小写,maxlen是比较的位数
用法:intstrncmpi(char*str1,char*str2,intmaxlen);
函数名:strncpy
功能:串拷贝 maxlen规定拷多少位
用法:char*strncpy(char*destin,char*source,intmaxlen);
函数名:strnicmp
功能:忽略大小写比较字符串 maxlen是比较的位数
用法:intstrnicmp(char*str1,char*str2,unsignedmaxlen);
函数名:strnset
功能:将一个字符串中的前n个字符都设为指定字符ch
用法:char*strnset(char*str,charch,unsignedn);
函数名:strpbrk
功能:在源字符串(s1)中找出最先含有搜索字符串(s2)中任一字符的位置并返回,若找不到则返回空指针
用法:char*strpbrk(char*str1,char*str2);
函数名:strrchr
功能:在串中查找指定字符的最后一个出现
用法:char*strrchr(char*str,charc);
函数名:strrev
功能:串倒转
用法:char*strrev(char*str);
函数名:strset
功能:将一个串中的所有字符都设为指定字符
用法:char*strset(char*str,charc);
函数名:strspn
功能:返回字符串str1中第一个不在指定字符串str2中出现的字符下标
用法:intstrspn(char*str1,char*str2);
函数名:strstr
功能:在串中查找指定字符串的第一次出现
用法:char*strstr(char*str1,char*str2);
函数名:strtok
功能:查找由在第二个串中指定的分界符分隔开的单词
用法:char*strtok(char*str1,char*str2);
程序例:
#include#include usingnamespacestd; intmain() { charsentence[]="Thisisasentencewith7tokens"; cout<<"Thestringtobetokenizedis:\n"< 函数名:strupr
功能:将串中的小写字母转换为大写字母
用法:char*strupr(char*str);函数名:swab
功能:交换字节,nbytes是交换的字节数
用法:voidswab(char*from,char*to,intnbytes);
程序例:#include#include #include charsource[15]="FrankBorland"; chartarget[15]; intmain(void) { swab(source,target,strlen(source)); printf("Thisistarget:%s\n",target); return0; } 到此就结束了,大家可以查看下面的几篇文章。