C語言標準庫中一個常用的頭檔案,在使用到字元數組時需要使用。
基本介紹
- 中文名:string.h
- 性質:函式定義的頭檔案
- 常見錯誤:載入異常
- 檔案大小:1.30 MB
簡單介紹
檔案資料
檔案名稱稱 | string.h |
檔案大小 | 1.30 MB |
系統檔案 | 否 |
後台執行 | 否 |
使用網路 | 否 |
硬體相關 | 否 |
常見錯誤 | 載入異常 |
記憶體使用 | 未知(N/A) |
安全等級 | 0 |
廣告軟體 | 否 |
間諜軟體 | 否 |
木馬病毒 | 否 |
編程軟體 | 是 |
函式館檔案 | 是 |
函式數目 | 26 |
版本內容
傳統 C++
標準 C++
C99 增加
疑問解答
函式用法
strcpy
#include<stdio.h>#include<string.h>int main(void){char string[10];char*str1="abcdefghi";strcpy(string,str1);printf("%s\n",string);return 0;}
strncpy
strcat
#include<string.h> #include<stdio.h>void main() { char destination[25]; char*blank="",*c="C++",*Borland="Borland"; strcpy(destination,Borland); strcat(destination,blank); strcat(destination,c); printf("%s\n",destination); }
strchr
#include<string.h>#include<stdio.h>int main(void){char string[15];char*ptr,c='r';strcpy(string,"Thisisastring");ptr=strchr(string,c);if(ptr)printf("Thecharacter%cisatposition:%d\n",c,ptr-string);elseprintf("Thecharacterwasnotfound\n");return 0;}
strcmp
#include<string.h>#include<stdio.h>int main(void){char*buf1="aaa",*buf2="bbb",*buf3="ccc";int ptr;ptr=strcmp(buf2,buf1);if(ptr>0)printf("buffer2isgreaterthanbuffer1\n");elseprintf("buffer2islessthanbuffer1\n");ptr=strcmp(buf2,buf3);if(ptr>0)printf("buffer2isgreaterthanbuffer3\n");elseprintf("buffer2islessthanbuffer3\n");return 0;}
strnicmp
#include<string.h>#include<stdio.h>int main(void){char*buf1="BBB",*buf2="bbb";int ptr;ptr=strnicmp(buf2,buf1);if(ptr>0)printf("buffer2isgreaterthanbuffer1\n");if(ptr<0)printf("buffer2islessthanbuffer1\n");if(ptr==0)printf("buffer2equalsbuffer1\n");return 0;}
strlen
#include<stdio.h>#include<string.h>int main(){int i=0;char*he="Hello,world";i=strlen(he);printf("字元串長度為%d\n",i);return 0;}
strcspn
#include<stdio.h>#include<string.h>int main(void) {char*string1="1234567890";char*string2="747DC8";int length;length=strcspn(string1,string2);printf("Characterwherestringsintersectisatposition%d\n",length);return 0;}
strdup
#include<stdio.h>#include<string.h>#include<alloc.h>int main(void){char*dup_str,*string="abcde";dup_str=strdup(string);printf("%s\n",dup_str);free(dup_str);return 0;}
stricmp
#include<string.h>#include<stdio.h>int main(void){char*buf1="BBB",*buf2="bbb";int ptr;ptr=stricmp(buf2,buf1);if(ptr>0)printf("buffer2isgreaterthanbuffer1\n");if(ptr<0)printf("buffer2islessthanbuffer1\n");if(ptr==0)printf("buffer2equalsbuffer1\n");return 0;}
strerror
#include<stdio.h>#include<errno.h>int main(void){char*buffer;buffer=strerror(errno);printf("Error:%s\n",buffer);return 0;}
strcmpi
#include<string.h>#include<stdio.h>int main(void){char*buf1="BBB",*buf2="bbb";int ptr;ptr=strcmpi(buf2,buf1);if(ptr>0)printf("buffer2 is greater than buffer1\n");if(ptr<0)printf("buffer2islessthanbuffer1\n");if(ptr==0)printf("buffer2equalsbuffer1\n");return 0;}
strncmp
#include<string.h>#include<stdio.h>int main(void){char *buf1="aaabbb",*buf2="bbbccc",*buf3="ccc";int ptr;ptr=strncmp(buf2,buf1,3);if(ptr>0)printf("buffer2 is greater than buffer1\n");elseprintf("buffer2 is less than buffer1\n");ptr=strncmp(buf2,buf3,3);if(ptr>0)printf("buffer2isgreaterthanbuffer3\n");elseprintf("buffer2islessthanbuffer3\n");return 0;}
strncpy
#include<stdio.h>#include<string.h>int main(void){char string[10];char*str1="abcdefghi";strncpy(string,str1,3);string[3]='\0';printf("%s\n",string);return 0;}
strnicmp
#include<string.h>#include<stdio.h>int main(void){char*buf1="BBBccc",*buf2="bbbccc";int ptr;ptr=strnicmp(buf2,buf1,3);if(ptr>0)printf("buffer2isgreaterthanbuffer1\n");if(ptr<0)printf("buffer2islessthanbuffer1\n");if(ptr==0)printf("buffer2equalsbuffer1\n");return 0;}
strnset
#include<stdio.h>#include<string.h>int main(void){char*string="abcdefghijklmnopqrstuvwxyz";char letter='x';printf("stringbeforestrnset:%s\n",string);strnset(string,letter,13);printf("stringafterstrnset:%s\n",string);return 0;}
strpbrk
#include<stdio.h>#include<string.h>int main(void){char*string1="abcdefghijklmnopqrstuvwxyz";char*string2="onm";char*ptr;ptr=strpbrk(string1,string2);if(ptr)printf("strpbrkfoundfirstcharacter:%c\n",*ptr);elseprintf("strpbrkdidn'tfindcharacterinset\n");return 0;}
strrchr
#include<string.h>#include<stdio.h>int main(void){char string[15];char*ptr,c='r';strcpy(string,"Thisisastring");ptr=strrchr(string,c);if(ptr)printf("Thecharacter%cisatposition:%d\n",c,ptr-string);elseprintf("Thecharacterwasnotfound\n");return 0;}
strrev
#include<string.h>#include<stdio.h>int main(void){char*forward="string";printf("Beforestrrev():%s\n",forward);strrev(forward);printf("Afterstrrev():%s\n",forward);return 0;}
strspn
#include<stdio.h>#include<string.h>#include<alloc.h>int main(void){char*string1="1234567890";char*string2="123DC8";int length;length=strspn(string1,string2);printf("Characterwherestringsdifferisatposition%d\n",length);return 0;}
strstr
#include<stdio.h>#include<string.h>int main(void){char*str1="BorlandInternational",*str2="nation",*ptr;ptr=strstr(str1,str2);printf("Thesubstringis:%s\n",ptr);return 0;}
strtod
#include<stdio.h>#include<stdlib.h>int main(void){char input[80],*endptr;double value;printf("Enterafloatingpointnumber:");gets(input);value=strtod(input,&endptr);printf("Thestringis%sthenumberis%lf\n",input,value);return 0;}
strtok
#include<string.h>#include<stdio.h>int main(void){char input[16]="abc,d";char*p;/*strtokplacesaNULLterminatorinfrontofthetoken,iffound*/p=strtok(input,",");if(p) printf("%s\n",p);/*AsecondcalltostrtokusingaNULLasthefirstparameterreturnsapointertothecharacterfollowingthetoken*/p=strtok(NULL,",");if(p) printf("%s\n",p);return 0;}
strtol
#include<stdlib.h>#include<stdio.h>int main(void){char*string="87654321",*endptr;long lnumber;/*strtolconvertsstringtolonginteger*/lnumber=strtol(string,&endptr,10);printf("string=%slong=%ld\n",string,lnumber);return 0;}
strupr
#include<stdio.h>#include<string.h>int main(void){char string[]="abcdefghijklmnopqrstuvwxyz",*ptr;//定義為數組才能修改/*convertsstringtouppercasecharacters*/ptr=strupr(string);printf("%s\n",ptr);return 0;}
swab
#include<stdlib.h>#include<stdio.h>#include<string.h>char source[15]="rFnakoBlrnad";char target[15];int main(void){swab(source,target,strlen(source));printf("Thisistarget:%s\n",target);return 0;}