C 字符串函数
bcmp(比較內存內容)?
相關函數???bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp
表頭文件???#include<string.h>
定義函數???int?bcmp?(?const?void?*s1,const?void?*?s2,int?n);
函數說明???bcmp()用來比較s1和s2所指的內存區間前n個字節,若參數n為0,則返回0。
返回值???若參數s1?和s2?所指的內存內容都完全相同則返回0?值,否則返回非零值。
附加說明???建議使用memcmp()取代。
范例???參考memcmp()。?
?
bcopy(拷貝內存內容)?
相關函數???memccpy,memcpy,memmove,strcpy,ctrncpy
表頭文件???#include?<string.h>
定義函數???void?bcopy?(?const?void?*src,void?*dest?,int?n);
函數說明???bcopy()與memcpy()一樣都是用來拷貝src所指的內存內容前n個字節到dest所指的地址,不過參數src與dest在傳給函數時是相反的位置。
返回值?附加說明???建議使用memcpy()取代
范例???#include<string.h>
main()
{
char?dest[30]=”string(a)”;
char?src[30]=”stringstring”;
int?i;
bcopy(src,dest,30);
printf(bcopy():?“)
for(i=0;i<30;i++)
printf(“%c”,dest[i]);
memcpy(dest?src,30);?
printf(‘nmemcpy()?:?“);
for(i=0;i<30;i++)
printf(“%c”,dest[i]);
}
執行???bcopy()?:?string?string
memcpy()?:string?sring
?
?
bzero(將一段內存內容全清為零)?
相關函數???memset,swab
表頭文件???#include<string.h>
定義函數???void?bzero(void?*s,int?n);
函數說明???bzero()會將參數s所指的內存區域前n個字節,全部設為零值。相當于調用memset((void*)s,0,size_tn);
返回值?附加說明???建議使用memset取代
范例???參考memset()。
?
?
index(查找字符串中第一個出現的指定字符)?
相關函數???rindex,srechr,strrchr
表頭文件???#include<string.h>
定義函數???char?*?index(?const?char?*s,?int?c);
函數說明???index()用來找出參數s字符串中第一個出現的參數c地址,然后將該字符出現的地址返回。字符串結束字符(NULL)也視為字符串一部分。
返回值???如果找到指定的字符則返回該字符所在地址,否則返回0。
范例???#include<string.h>
main()
{
char?*s?=”0123456789012345678901234567890”;
char?*p;
p?=index(s,’5’);
printf(%sn”,p);
}
執行???5.68E+25
?
?
memccpy(拷貝內存內容)?
相關函數???bcopy,memcpy,memmove,strcpy,strncpy
表頭文件???#include<string.h>
定義函數???void?*?memccpy(void?*dest,?const?void?*?src,?int?c,size_t?n);
函數說明???memccpy()用來拷貝src所指的內存內容前n個字節到dest所指的地址上。與memcpy()不同的是,memccpy()會在復制時檢查參數c是否出現,若是則返回dest中值為c的下一個字節地址。
返回值???返回指向dest中值為c的下一個字節指針。返回值為0表示在src所指內存前n個字節中沒有值為c的字節。
范例???#include<string.h>
main()
{
char?a[]="string[a]";
char?b[]="string(b)";
memccpy(a,b,'B',sizeof(b));
printf("memccpy():%sn",a);
}
執行???memccpy():string(b)
?
?
memchr(在某一內存范圍中查找一特定字符)?
相關函數???index,rindex,strchr,strpbrk,strrchr,strsep,strspn,strstr
表頭文件???#include<string.h>
定義函數???void?*?memchr(const?void?*s,int?c,size_t?n);
函數說明???memchr()從頭開始搜尋s所指的內存內容前n個字節,直到發現第一個值為c的字節,則返回指向該字節的指針。
返回值???如果找到指定的字節則返回該字節的指針,否則返回0。
范例???#include?<string.h>
main()
{
char?*s="0123456789012345678901234567890";
char?*p;
p=memchr(s,'5',10);
printf("%sn",p);
}
執行???5.68E+25
?
?
memcmp(比較內存內容)?
相關函數???bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp
表頭文件???#include<string.h>
定義函數???int?memcmp?(const?void?*s1,const?void?*s2,size_t?n);
函數說明???memcmp()用來比較s1和s2所指的內存區間前n個字符。字符串大小的比較是以ASCII碼表上的順序來決定,次順序亦為字符的值。memcmp()首先將s1第一個字符值減去s2第一個字符的值,若差為0則再繼續比較下個字符,若差值不為0則將差值返回。例如,字符串"Ac"和"ba"比較則會返回字符'A'(65)和'b'(98)的差值(-33)。
返回值???若參數s1和s2所指的內存內容都完全相同則返回0值。s1若大于s2則返回大于0的值。s1若小于s2則返回小于0的值。
范例???#include<string.h>
main()
{
char?*a?="aBcDeF";
char?*b="AbCdEf";
char?*c="aacdef";
char?*d="aBcDeF";
printf("memcmp(a,b):%dn",memcmp((void*)a,(void*)?b,6));
printf("memcmp(a,c):%dn",memcmp((void*)a,(void*)?c,6));
printf("memcmp(a,d):%dn",memcmp((void*)a,(void*)?d,6));
}
執行???memcmp(a,b):1?
memcmp(a,c):-1?
memcmp(a,d):0?
?
?
memcpy(拷貝內存內容)?
相關函數???bcopy,memccpy,memcpy,memmove,strcpy,strncpy
表頭文件???#include<string.h>
定義函數???void?*?memcpy?(void?*?dest?,const?void?*src,?size_t?n);
函數說明???memcpy()用來拷貝src所指的內存內容前n個字節到dest所指的內存地址上。與strcpy()不同的是,memcpy()會完整的復制n個字節,不會因為遇到字符串結束''而結束。
返回值???返回指向dest的指針。
附加說明???指針src和dest所指的內存區域不可重疊。
范例???#include<string.h>
main()
{
char?a[30]="string?(a)";
char?b[30]="stringstring";
int?i;
strcpy(a,b);
printf("strcpy():");
for(i=0;i<30;i++)
printf("%c",a[i]);
memcpy(a,b,30);
printf("nmemcpy()?:");
for(i=0;i<30;i++)
printf("%c",a[i]);
}
執行???strcpy()?:?string?a?)
memcpy()?:?string?string
?
?
memmove(拷貝內存內容)?
相關函數???bcopy,memccpy,memcpy,strcpy,strncpy
表頭文件???#include<string.h>
定義函數???void?*?memmove(void?*dest,const?void?*src,size_t?n);
函數說明???memmove()與memcpy()一樣都是用來拷貝src所指的內存內容前n個字節到dest所指的地址上。不同的是,當src和dest所指的內存區域重疊時,memmove()仍然可以正確的處理,不過執行效率上會比使用memcpy()略慢些。
返回值???返回指向dest的指針。
附加說明???指針src和dest所指的內存區域可以重疊。
范例???參考memcpy()。
?
?
memset(將一段內存空間填入某值)?
相關函數???bzero,swab
表頭文件???#include<string.h>
定義函數???void?*?memset?(void?*s?,int?c,?size_t?n);
函數說明???memset()會將參數s所指的內存區域前n個字節以參數c填入,然后返回指向s的指針。在編寫程序時,若需要將某一數組作初始化,memset()會相當方便。
返回值???返回指向s的指針。
附加說明???參數c雖聲明為int,?但必須是unsigned?char?,所以范圍在0到255之間。
范例???#include?<string.h>
main()
{
char?s[30];
memset?(s,'A',sizeof(s));
s[30]='';
printf("%sn",s);
}
執行???AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
?
?
rindex(查找字符串中最后一個出現的指定字符)?
相關函數???index,memchr,strchr,strrchr
表頭文件???#include<string.h>
定義函數???char?*?rindex(?const?char?*s,int?c);
函數說明???rindex()用來找出參數s字符串中最后一個出現的參數c地址,然后將該字符出現的地址返回。字符串結束字符(NULL)也視為字符串一部分。
返回值???如果找到指定的字符則返回該字符所在的地址,否則返回0。
范例???#include?<string.h>
mian()
{
char?*s?="0123456789012345678901234567890";
char?*p;
p=rindex(s,'5');
printf("%sn",p);
}
執行???567890
?
?
strcasecmp(忽略大小寫比較字符串)?
相關函數???bcmp,memcmp,strcmp,strcoll,strncmp
表頭文件???#include<string.h>
定義函數???int?strcasecmp?(const?char?*s1,?const?char?*s2);
函數說明???strcasecmp()用來比較參數s1和s2字符串,比較時會自動忽略大小寫的差異。
返回值???若參數s1和s2字符串相同則返回0。s1長度大于s2長度則返回大于0?的值,s1?長度若小于s2?長度則返回小于0的值。
范例???#include?<string.h>
main()
{
char?*a="aBcDeF";
char?*b="AbCdEf";
if(!strcasecmp(a,b))
printf("%s=%sn",a,b);
}
執行???aBcDeF=AbCdEf
?
?
strcat(連接兩字符串)?
相關函數???bcopy,memccpy,memcpy,strcpy,strncpy
表頭文件???#include?<string.h>
定義函數???char?*strcat?(char?*dest,const?char?*src);
函數說明???strcat()會將參數src字符串拷貝到參數dest所指的字符串尾。第一個參數dest要有足夠的空間來容納要拷貝的字符串。
返回值???返回參數dest的字符串起始地址
范例???#include?<string.h.>
main()
{
char?a[30]="string(1)";
char?b[]="string(2)";
printf("before?strcat()?:?%sn",a);
printf("after?strcat()?:?%sn",strcat(a,b));
}
執行???before?strcat?()?:?string(1)
after?strcat?()?:?string(1)string(2)
?
?
strchr(查找字符串中第一個出現的指定字符)?
相關函數???index,memchr,rinex,strbrk,strsep,strspn,strstr,strtok
表頭文件???#include<string.h>
定義函數???char?*?strchr?(const?char?*s,int?c);
函數說明???strchr()用來找出參數s字符串中第一個出現的參數c地址,然后將該字符出現的地址返回。
返回值???如果找到指定的字符則返回該字符所在地址,否則返回0。
范例???#include<string.h>
main()
{
char?*s=0123456789012345678901234567890”;
char?*p;
p=strchr(s,'5');
printf("%sn",p);
}
執行???5.68E+25
?
?
strcmp(比較字符串)?
相關函數???bcmp,memcmp,strcasecmp,strncasecmp,strcoll
表頭文件???#include<string.h>
定義函數???int?strcmp(const?char?*s1,const?char?*s2);
函數說明???strcmp()用來比較參數s1和s2字符串。字符串大小的比較是以ASCII?碼表上的順序來決定,此順序亦為字符的值。strcmp()首先將s1第一個字符值減去s2第一個字符值,若差值為0則再繼續比較下個字符,若差值不為0則將差值返回。例如字符串"Ac"和"ba"比較則會返回字符"A"(65)和'b'(98)的差值(-33)。
返回值???若參數s1和s2字符串相同則返回0。s1若大于s2則返回大于0的值。s1若小于s2則返回小于0?的值。
范例???#include<string.h>
main()
{
char?*a="aBcDeF";
char?*b="AbCdEf";
char?*c="aacdef";
char?*d="aBcDeF";
printf("strcmp(a,b)?:?%dn",strcmp(a,b));
printf("strcmp(a,c)?:?%dn",strcmp(a,c));
printf("strcmp(a,d)?:?%dn",strcmp(a,d));
}
執行???strcmp(a,b)?:?32
strcmp(a,c)?:-31
strcmp(a,d)?:?0
?
?
strcoll(采用目前區域的字符排列次序來比較字符串)?
相關函數???strcmp,bcmp,memcmp,strcasecmp,strncasecmp
表頭文件???#include<string.h>
定義函數???int?strcoll(?const?char?*s1,?const?char?*s2);
函數說明???strcoll()會依環境變量LC_COLLATE所指定的文字排列次序來比較s1和s2?字符串。
返回值???若參數s1和s2字符串相同則返回0。s1若大于s2則返回大于0的值。s1若小于s2則返回小于0?的值。
附加說明???若LC_COLLATE為"POSIX"或"C",則strcoll()與strcmp()作用完全相同。
范例???參考strcmp()。
?
?
strcpy(拷貝字符串)?
相關函數???bcopy,memcpy,memccpy,memmove
表頭文件???#include<string.h>
定義函數???char?*strcpy(char?*dest,const?char?*src);
函數說明???strcpy()會將參數src字符串拷貝至參數dest所指的地址。
返回值???返回參數dest的字符串起始地址。
附加說明???如果參數dest所指的內存空間不夠大,可能會造成緩沖溢出(buffer?Overflow)的錯誤情況,在編寫程序時請特別留意,或者用strncpy()來取代。
范例???#include<string.h>
main()
{
char?a[30]="string(1)";
char?b[]="string(2)";
printf("before?strcpy()?:%sn",a);
printf("after?strcpy()?:%sn",strcpy(a,b));
}
執行???before?strcpy()?:string(1)
after?strcpy()?:string(2)
?
?
strcspn(返回字符串中連續不含指定字符串內容的字符數)?
相關函數???strspn
表頭文件???#inclued<string.h>
定義函數???size_t?strcspn?(?const?char?*s,const?char?*?reject);
函數說明???strcspn()從參數s字符串的開頭計算連續的字符,而這些字符都完全不在參數reject?所指的字符串中。簡單地說,若strcspn()返回的數值為n,則代表字符串s開頭連續有n個字符都不含字符串reject內的字符。
返回值???返回字符串s開頭連續不含字符串reject內的字符數目。
范例???#include?<string.h>
main()
{
char?*str="Linux?was?first?developed?for?386/486-based?pcs.";
printf("%dn",strcspn(str,"?"));
printf("%dn",strcspn(str,"/-"));
printf("%dn",strcspn(str,"1234567890"));
}
執行???5?
33?
30?
?
?
strdup(復制字符串)?
相關函數???calloc,malloc,realloc,free
表頭文件???#include<string.h>
定義函數???char?*?strdup(?const?char?*s);
函數說明???strdup()會先用maolloc()配置與參數s字符串相同的空間大小,然后將參數s字符串的內容復制到該內存地址,然后把該地址返回。該地址最后可以利用free()來釋放。
返回值???返回一字符串指針,該指針指向復制后的新字符串地址。若返回NULL表示內存不足。
范例???#include<string.h>
main()
{
char?a[]="strdup";
char?*b;
b=strdup(a);
printf("b[?]="%s"n",b);
}
執行???b[?]="strdup"
?
?
strlen(返回字符串長度)?
相關函數?表頭文件???#include<string.h>
定義函數???size_t?strlen?(const?char?*s);
函數說明???strlen()用來計算指定的字符串s的長度,不包括結束字符""。
返回值???返回字符串s的字符數。
范例???
#include<string.h>
main()
{
char?*str?=?"12345678";
printf("str?length?=?%dn",?strlen(str));
}
執行???str?length?=?8
?
?
strncasecmp(忽略大小寫比較字符串)?
相關函數???bcmp,memcmp,strcmp,strcoll,strncmp
表頭文件???#include<string.h>
定義函數???int?strncasecmp(const?char?*s1,const?char?*s2,size_t?n);
函數說明???strncasecmp()用來比較參數s1和s2字符串前n個字符,比較時會自動忽略大小寫的差異。
返回值???若參數s1和s2?字符串相同則返回0。s1?若大于s2則返回大于0的值,s1若小于s2則返回小于0?的值。
范例???#include<string.h>
main()
{
char?*a="aBcDeF";
char?*b="AbCdEf";
if(!strncasecmp(a,b))
printf("%s?=%sn",a,b);
}
執行???aBcDef=AbCdEf
?
?
strncat(連接兩字符串)?
相關函數???bcopy,memccpy,memecpy,strcpy,strncpy
表頭文件???#inclue?<string.h>
定義函數???char?*?strncat(char?*dest,const?char?*src,size_t?n);
函數說明???strncat()會將參數src字符串拷貝n個字符到參數dest所指的字符串尾。第一個參數dest要有足夠的空間來容納要拷貝的字符串。
返回值???返回參數dest的字符串起始地址。
范例???#include?<string.h>
main()
{
char?a[30]="string(1)";
char?b[]="string(2)";
printf("before?strnact()?:%sn",?a);
printf("after?strncat()?:%sn",?strncat(a,b,6));
}
執行???before?strnact()?:?string(1)
after?strncat()?:?string(1)?string
?
?
strncpy(拷貝字符串)?
相關函數???bcopy,memccpy,memcpy,memmove
表頭文件???#include<string.h>
定義函數???char?*?strncpy(char?*dest,const?char?*src,size_t?n);
函數說明???strncpy()會將參數src字符串拷貝前n個字符至參數dest所指的地址。
返回值???返回參數dest的字符串起始地址。
范例???#inclue?<string.h>
main()
{
char?a[30]="string(1)";
char?b[]="string(2)";
printf("before?strncpy()?:?%sn",a);
printf("after?strncpy()?:?%sn",strncpy(a,b,6));
}
執行???before?strncpy()?:?string(1)
after?strncpy()?:?string(1)
?
?
strpbrk(查找字符串中第一個出現的指定字符)?
相關函數???index,memchr,rindex,strpbrk,strsep,strspn,strstr,strtok
表頭文件???#include?<include.h>
定義函數???char?*strpbrk(const?char?*s,const?char?*accept);
函數說明???strpbrk()用來找出參數s?字符串中最先出現存在參數accept?字符串中的任意字符。
返回值???如果找到指定的字符則返回該字符所在地址,否則返回0。
范例???#include?<string.h>
main()
{
char?*s="0123456789012345678901234567890";
char?*p;
p=strpbrk(s,"a1?839");?
printf("%sn",p);
p=strprk(s,"4398");
printf("%sn",p);
執行???1.23E+29
?
?
strrchr(查找字符串中最后出現的指定字符)
相關函數???index,memchr,rindex,strpbrk,strsep,strspn,strstr,strtok
表頭文件???#include<string.h>
定義函數???char?*?strrchr(const?char?*s,?int?c);
函數說明???strrchr()用來找出參數s字符串中最后一個出現的參數c地址,然后將該字符出現的地址返回。
返回值???如果找到指定的字符則返回該字符所在地址,否則返回0。
范例???#include<string.h>
main()
{
char?*s="0123456789012345678901234567890";
char?*p;
p=strrchr(s,'5');
printf("%sn",p);
}
執行???567890
?
?
strspn(返回字符串中連續不含指定字符串內容的字符數)?
相關函數???strcspn,strchr,strpbrk,strsep,strstr
表頭文件???#include<string.h>
定義函數???size_t?strspn?(const?char?*s,const?char?*?accept);
函數說明???strspn()從參數s?字符串的開頭計算連續的字符,而這些字符都完全是accept?所指字符串中的字符。簡單的說,若strspn()返回的數值為n,則代表字符串s?開頭連續有n?個字符都是屬于字符串accept內的字符。
返回值???返回字符串s開頭連續包含字符串accept內的字符數目。
范例???#include<string.h>
main()
{
char?*str="Linux?was?first?developed?for?386/486-based?PCs.";
char?*t1="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%dn",strspn(str,t1));
}
執行???5?
?
?
strstr(在一字符串中查找指定的字符串)?
相關函數???index,memchr,rindex,strchr,strpbrk,strsep,strspn,strtok
表頭文件???#include<string.h>
定義函數???char?*strstr(const?char?*haystack,const?char?*needle);
函數說明???strstr()會從字符串haystack?中搜尋字符串needle,并將第一次出現的地址返回。
返回值???返回指定字符串第一次出現的地址,否則返回0。
范例???#include<string.h>
main()
{
char?*?s="012345678901234567890123456789";
char?*p;
p=?strstr(s,"901");
printf("%sn",p);
}
執行???9.01E+21
?
?
strtok(分割字符串)?
相關函數???index,memchr,rindex,strpbrk,strsep,strspn,strstr
表頭文件???#include<string.h>
定義函數???char?*?strtok(char?*s,const?char?*delim);
函數說明???strtok()用來將字符串分割成一個個片段。參數s指向欲分割的字符串,參數delim則為分割字符串,當strtok()在參數s的字符串中發現到參數delim的分割字符時則會將該字符改為?字符。在第一次調用時,strtok()必需給予參數s字符串,往后的調用則將參數s設置成NULL。每次調用成功則返回下一個分割后的字符串指針。
返回值???返回下一個分割后的字符串指針,如果已無從分割則返回NULL。
范例???#include<string.h>
main()
{
char?s[]="ab-cd?:?ef;gh?:i-jkl;mnop;qrs-tu:?vwx-y;z";
char?*delim="-:?";
char?*p;
printf("%s?";strtok(s,delim));
while((p=strtok(NULL,delim)))printf("%s?",p);
printf("n");
}
轉載于:https://www.cnblogs.com/E-star/archive/2012/11/27/2791781.html
總結
- 上一篇: 使用Nexus2.x为Maven3.x搭
- 下一篇: M2第三天DailyScrum——PM(