C語言中沒有string類型。
創(chuàng)新互聯(lián)公司長期為上千多家客戶提供的網站建設服務,團隊從業(yè)經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網生態(tài)環(huán)境。為王益企業(yè)提供專業(yè)的網站制作、網站建設,王益網站改版等技術服務。擁有10多年豐富建站經驗和眾多成功案例,為您定制開發(fā)。
string類型是 C++、java、VB等編程語言中的。 在java、C#中,String類是不可變的,對String類的任何改變,都是返回一個新的String類對象。string 是C++標準程序庫中的一個頭文件,定義了C++標準中的字符串的基本模板類std::basic_string及相關的模板類實例。
而在C語言中,C語言中沒有字符串類型,字符串是存放在字符型數(shù)組中的,將字符串作為字符數(shù)組來處理的。為了測定字符串的實際長度,C語言規(guī)定了一個“字符串結束標志”,以字符'\0'作為結束標志 。
擴展資料:
C++中string的使用:
其中的string是以char作為模板參數(shù)的模板類實例,把字符串的內存管理責任由string負責而不是由編程者負責,大大減輕了C語言風格的字符串的麻煩。std::basic_string提供了大量的字符串操作函數(shù),如比較、連接、搜索、替換、獲得子串等。并可與C語言風格字符串雙向轉換。
字符串在C語言中的使用:
字符串常量初始化數(shù)組。如:char c[ ]={‘c’,‘ ’,‘p’,‘r’,‘o','g','r','a','m'};可寫為:char c[ ]={"C program"}; 或去掉{}寫為:char c[ ]="C program"。
參考資料來源:百度百科-string類
參考資料來源:百度百科-字符數(shù)組
string在c語言中的意思是System.Char對象的有序集合,用于表示字符串。
String就是C等編程語言中的字符串,用雙引號引起來的幾個字符。
在java、C#中,String類是不可變的,對String類的任何改變,都是返回一個新的String類對象。String對象是?System.Char?對象的有序集合,用于表示字符串。String對象的值是該有序集合的內容,并且該值是不可變的。
可以使用以下方法之一來創(chuàng)建string對象:
通過給String變量指定一個字符串。
通過使用String類構造函數(shù)。
通過使用字符串串聯(lián)運算符(+)。
通過檢索屬性或調用一個返回字符串的方法。
通過格式化方法來轉換一個值或對象為它的字符串表示形式。
C語言是一門面向過程的、抽象化的通用程序設計語言,廣泛應用于底層開發(fā)。C語言能以簡易的方式編譯、處理低級存儲器。C 語言string的用法有哪些呢,請看看下面我為你整理 總結 的c語言string的用法大全_C語言中string使用 方法 。
c語言string的用法
函數(shù)原型:char *strdup(const char *s)
函數(shù)功能:字符串拷貝,目的空間由該函數(shù)分配
函數(shù)返回:指向拷貝后的字符串指針
參數(shù)說明:src-待拷貝的源字符串
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includestring.h
#includealloc.h
intmain()
{
char*dup_str,*string="abcde";
dup_str=strdup(string);
printf("%s",dup_str);
free(dup_str);
return0;
}
@函數(shù)名稱:strcpy
函數(shù)原型:char* strcpy(char* str1,char* str2);
函數(shù)功能:把str2指向的字符串拷貝到str1中去
函數(shù)返回:返回str1,即指向str1的指針
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
charstring[10];
char*str1="abcdefghi";
strcpy(string,str1);
printf("thestringis:%s\n",string);
return0;
}
@函數(shù)名稱:strncpy
函數(shù)原型:char *strncpy(char *dest, const char *src,intcount)
函數(shù)功能:將字符串src中的count個字符拷貝到字符串dest中去
函數(shù)返回:指向dest的指針
參數(shù)說明:dest-目的字符串,src-源字符串,count-拷貝的字符個數(shù)
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
char*src="bbbbbbbbbbbbbbbbbbbb";//20'b's
chardest[50]="aaaaaaaaaaaaaaaaaaaa";//20'a's
puts(dest);
strncpy(dest,src,10);
puts(dest);
return0;
}
輸出:
[cpp] view plain
/*******************************************
aaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbaaaaaaaaaa
*******************************************/
注意:strncpy只復制指定長度的字符,不會自動在末尾加'\0'。若指定長度超過源字符串長度,不夠的部分補‘\0’,
@函數(shù)名稱:strcat
函數(shù)原型:char* strcat(char * str1,char * str2);
函數(shù)功能:把字符串str2接到str1后面,str1最后的'\0'被取消
函數(shù)返回:str1
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
charbuffer[80];
strcpy(buffer,"Hello");
strcat(buffer,"world");
printf("%s\n",buffer);
return0;
}
@函數(shù)名稱:strncat
函數(shù)原型:char *strncat(char *dest, const char *src, size_t maxlen)
函數(shù)功能:將字符串src中前maxlen個字符連接到dest中
函數(shù)返回:
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includestring.h
charbuffer[80];
intmain()
{
strcpy(buffer,"Hello");
strncat(buffer,"world",8);
printf("%s\n",buffer);
strncat(buffer,"*************",4);
printf("%s\n",buffer);
return0;
}
注意:與strncpy不同的是,strncat會自動在末尾加‘\0’,若指定長度超過源字符串長度,則只復制源字符串長度即停止
@函數(shù)名稱:strcmp
函數(shù)原型:int strcmp(char * str1,char * str2);
函數(shù)功能:比較兩個字符串str1,str2.
函數(shù)返回:str1str2,返回負數(shù);str1=str2,返回 0;str1str2,返回正數(shù).
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestring.h
#includestdio.h
intmain()
{
char*buf1="aaa",*buf2="bbb",*buf3="ccc";
intptr;
ptr=strcmp(buf2,buf1);
if(ptr0)
printf("buffer2isgreaterthanbuffer1\n");
else
printf("buffer2islessthanbuffer1\n");
ptr=strcmp(buf2,buf3);
if(ptr0)
printf("buffer2isgreaterthanbuffer3\n");
else
printf("buffer2islessthanbuffer3\n");
return0;
}
@函數(shù)名稱:strncmp
函數(shù)原型:int strncmp(char *str1,char *str2,int count)
函數(shù)功能:對str1和str2中的前count個字符按字典順序比較
函數(shù)返回:小于0:str1str2,等于0:str1=str2,大于0:str1str2
參數(shù)說明:str1,str2-待比較的字符串,count-比較的長度
所屬文件:string.h
[cpp] view plain
#includestring.h
#includestdio.h
intmain()
{
charstr1[]="aabbc";//
charstr2[]="abbcd";//
//為使測試程序更簡練,此處假定了strncmp只返回-1,0,1三個數(shù)
charres_info[]={'','=',''};
intres;
//前1個字符比較
res=strncmp(str1,str2,1);
printf("1:str1%cstr2\n",res_info[res+1]);
//前3個字符比較
res=strncmp(str1,str2,3);
printf("3:str1%cstr2\n",res_info[res+1]);
}
輸出:
[cpp] view plain
/****************************************
1:str1=str2
3:str1str2
*****************************************/
@函數(shù)名稱:strpbrk
函數(shù)原型:char *strpbrk(const char *s1, const char *s2)
函數(shù)功能:得到s1中第一個“同時也出現(xiàn)在s2中”字符的位置指針
函數(shù)返回:位置指針
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
char*p="Findallvowels";
p=strpbrk(p+1,"aeiouAEIOU");
while(p)
{
printf("%s\n",p);
p=strpbrk(p+1,"aeiouAEIOU");
}
return0;
}
輸出:
[cpp] view plain
/**************************************
indallvowels
allvowels
owels
els
**************************************/
@函數(shù)名稱:strcspn
函數(shù)原型:int strcspn(const char *s1, const char *s2)
函數(shù)功能:統(tǒng)計s1中從頭開始直到第一個“來自s2中的字符”出現(xiàn)的長度
函數(shù)返回:長度
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
printf("%d\n",strcspn("abcbcadef","cba"));
printf("%d\n",strcspn("xxxbcadef","cba"));
printf("%d\n",strcspn("123456789","cba"));
return0;
}
輸出:
[cpp] view plain
/************************
3
9
************************/
@函數(shù)名稱:strspn
函數(shù)原型:int strspn(const char *s1, const char *s2)
函數(shù)功能:統(tǒng)計s1中從頭開始直到第一個“不來自s2中的字符”出現(xiàn)的長度
函數(shù)返回:位置指針
參數(shù)說明:
所屬文件:string.h
[html] view plain
#includestdio.h
#includestring.h
#includealloc.h
intmain()
{
printf("%d\n",strspn("abcbcadef","cba"));
printf("%d\n",strspn("xxxbcadef","cba"));
printf("%d\n",strspn("123456789","cba"));
return0;
}
輸出:
[cpp] view plain
/************************
6
************************/
@函數(shù)名稱:strchr
函數(shù)原型:char* strchr(char* str,char ch);
函數(shù)功能:找出str指向的字符串中第一次出現(xiàn)字符ch的位置
函數(shù)返回:返回指向該位置的指針,如找不到,則返回空指針
參數(shù)說明:str-待搜索的字符串,ch-查找的字符
所屬文件:string.h
[cpp] view plain
#includestring.h
#includestdio.h
intmain()
{
char*str="Thisisastring!";
charch;
char*p;
while(1)
{
printf("Pleaseinputachar:");
ch=getchar();
p=strchr(str,ch);
if(p)
printf("%cisthe%dcharacterof\"%s\"\n",ch,(int)(p-str+1),str);
else
printf("Notfound!\n");
printf("PressESCtoquit!\n\n");
if(27==getch())
break;
fflush(stdin);
}
return0;
}
運行結果:
[cpp] view plain
/********************************************
Pleaseinputachar:i
iisthe3characterof"Thisisastring!"
PressESCtoquit!
Pleaseinputachar:l
Notfound!
PressESCtoquit!
Pleaseinputachar:s
sisthe4characterof"Thisisastring!"
PressESCtoquit!
**********************************************/
@函數(shù)名稱:strrchr
函數(shù)原型:char *strrchr(const char *s, int c)
函數(shù)功能:得到字符串s中最后一個含有c字符的位置指針
函數(shù)返回:位置指針
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestring.h
#includestdio.h
intmain()
{
charstring[15];
char*ptr,c='r';
strcpy(string,"Thisisastring");
ptr=strrchr(string,c);
if(ptr)
printf("Thecharacter%cisatposition:%d",c,ptr-string);
else
printf("Thecharacterwasnotfound");
return0;
}
@函數(shù)名稱:strstr
函數(shù)原型:char* strstr(char* str1,char* str2);
函數(shù)功能:找出str2字符串在str1字符串中第一次出現(xiàn)的位置(不包括str2的串結束符)
函數(shù)返回:返回該位置的指針,如找不到,返回空指針
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
char*str1="OpenWatcomC/C++",*str2="Watcom",*ptr;
ptr=strstr(str1,str2);
printf("Thesubstringis:%s\n",ptr);
return0;
}
輸出:
The substringis:Watcom C/C++
@函數(shù)名稱:strrev
函數(shù)原型:char *strrev(char *s)
函數(shù)功能:將字符串中的所有字符顛倒次序排列
函數(shù)返回:指向s的指針
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestring.h
#includestdio.h
intmain()
{
charforward[]="string";//原文中定義為char*是不對的,指向代碼段的指針內容是不可變的
printf("Beforestrrev():%s",forward);
strrev(forward);
printf("Afterstrrev():%s",forward);
return0;
}
輸出:
[cpp] view plain
/************************************
Beforestrrev():string
Afterstrrev():gnirts
************************************/
@函數(shù)名稱:strnset
函數(shù)原型:char *strnset(char *s, int ch, size_t n)
函數(shù)功能:將字符串s中前n個字符設置為ch的值
函數(shù)返回:指向s的指針
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
charstring[]="aaaaaaaaaaaaaaaaaaaaaaa";
charletter='x';
printf("stringbeforestrnset:%s\n",string);
strnset(string,letter,10);
printf("stringafterstrnset:%s\n",string);
return0;
}
輸出:
[cpp] view plain
/*************************************************
stringbeforestrnset:aaaaaaaaaaaaaaaaaaaaaaa
stringafterstrnset:xxxxxxxxxxaaaaaaaaaaaaa
*************************************************/
@函數(shù)名稱:strset
函數(shù)原型:char *strset(char *s, int ch)
函數(shù)功能:將字符串s中所有字符設置為ch的值
函數(shù)返回:指向s的指針
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
charstring[10]="123456789";
charsymbol='c';
printf("Beforestrset():%s",string);
strset(string,symbol);
printf("Afterstrset():%s",string);
return0;
}
@函數(shù)名稱:strtok
函數(shù)原型:char *strtok(char *s1, const char *s2)
函數(shù)功能:分解s1字符串為用特定分隔符分隔的多個字符串(一般用于將英文句分解為單詞)
函數(shù)返回:字符串s1中首次出現(xiàn)s2中的字符前的子字符串指針
參數(shù)說明:s2一般設置為s1中的分隔字符
規(guī)定進行子調用時(即分割s1的第二、三及后續(xù)子串)第一參數(shù)必須是NULL
在每一次匹配成功后,將s1中分割出的子串位置替換為NULL(摘下鏈中第一個環(huán)),因此s1被破壞了
函數(shù)會記憶指針位置以供下一次調用
所屬文件:string.h
[cpp] view plain
#includestring.h
#includestdio.h
intmain()
{
char*p;
char*buffer;
char*delims={".,"};
buffer=strdup("Findwords,allofthem.");
printf("%s\n",buffer);
p=strtok(buffer,delims);
while(p!=NULL){
printf("word:%s\n",p);
p=strtok(NULL,delims);
}
printf("%s\n",buffer);
return0;
}//根據(jù)測試,可以隨時給strtok的第一個參數(shù)輸入一個新的字符串,開始新字符串的分隔
PS:根據(jù)測試,可以隨時給strtok的第一個參數(shù)輸入一個新的字符串,開始新字符串的分隔
@函數(shù)名稱:strupr
函數(shù)原型:char *strupr(char *s)
函數(shù)功能:將字符串s中的字符變?yōu)榇髮?/p>
函數(shù)返回:
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
charstring[]="abcdefghijklmnopqrstuvwxyz",*ptr;//會影響原字符串的內存,用char[]來聲明
ptr=strupr(string);
printf("%s",ptr);
return0;
}
@函數(shù)名稱:strlwr
函數(shù)原型:char *strlwr(char *s)
函數(shù)功能:將字符串中的字符變?yōu)樾懽址?/p>
函數(shù)返回:指向s的指針
參數(shù)說明:
所屬文件:string.h
[cpp] view plain
#includestring.h
intmain()
{
charstr[]="HOWTOSAY";
printf("%s",strlwr(str));
return0;
}
@函數(shù)名稱:strerror
函數(shù)原型:char *strerror(int errnum)
函數(shù)功能:得到錯誤信息的內容信息
函數(shù)返回:錯誤提示信息字符串指針
參數(shù)說明:errnum-錯誤編號
所屬文件:string.h
[cpp] view plain
#includestdio.h
#includeerrno.h
intmain()
{
char*buffer;
buffer=strerror(errno);
printf("Error:%s",buffer);
return0;
}
@函數(shù)名稱:memcpy
函數(shù)原型:void *memcpy(void *dest, const void *src, size_t n)
函數(shù)功能:字符串拷貝
函數(shù)返回:指向dest的指針
參數(shù)說明:src-源字符串,n-拷貝的最大長度
所屬文件:string.h,mem.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
charsrc[]="******************************";
chardest[]="abcdefghijlkmnopqrstuvwxyz0123456709";
char*ptr;
printf("destinationbeforememcpy:%s\n",dest);
ptr=memcpy(dest,src,strlen(src));
if(ptr)
printf("destinationaftermemcpy:%s\n",dest);
else
printf("memcpyfailed");
return0;
}
輸出:
[cpp] view plain
/*************************************************************
destinationbeforememcpy:abcdefghijlkmnopqrstuvwxyz0123456709
destinationaftermemcpy:******************************456709
**************************************************************/
@函數(shù)名稱:memccpy
函數(shù)原型:void *memccpy(void *dest, const void *src, int c, size_t n)
函數(shù)功能:字符串拷貝,到指定長度或遇到指定字符時停止拷貝
函數(shù)返回:
參數(shù)說明:src-源字符串指針,c-中止拷貝檢查字符,n-長度,dest-拷貝底目的字符串指針
所屬文件:string.h,mem.h
[cpp] view plain
#includestring.h
#includestdio.h
intmain()
{
char*src="Thisisthesourcestring";
chardest[50];
char*ptr;
ptr=memccpy(dest,src,'c',strlen(src));
if(ptr)
{
*ptr='\0';
printf("Thecharacterwasfound:%s",dest);
}
else
printf("Thecharacterwasn'tfound");
return0;
}
輸出:
[cpp] view plain
/*****************************************
Thecharacterwasfound:Thisisthesourc
*****************************************/
PS:指定字符被復制到dest中,memccpy返回了dest中指定字符的下一處的地址,返回NULL表示未遇到指定字符
@函數(shù)名稱:memchr
函數(shù)原型:void *memchr(const void *s, int c, size_t n)
函數(shù)功能:在字符串中第開始n個字符中尋找某個字符c的位置
函數(shù)返回:返回c的位置指針,返回NULL時表示未找到
參數(shù)說明:s-要搜索的字符串,c-要尋找的字符,n-指定長度
所屬文件:string.h,mem.h
[cpp] view plain
#includestring.h
#includestdio.h
intmain()
{
charstr[17];
char*ptr;
strcpy(str,"Thisisastring");
ptr=memchr(str,'r',strlen(str));
if(ptr)
printf("Thecharacter'r'isatposition:%d",ptr-str);
else
printf("Thecharacterwasnotfound");
return0;
}
@函數(shù)名稱:memcmp
函數(shù)原型:int memcmp(const void *s1, const void *s2,size_t n)
函數(shù)功能:按字典順序比較兩個串s1和s2的前n個字節(jié)
函數(shù)返回:0,=0,0分別表示s1,=,s2
參數(shù)說明:s1,s2-要比較的字符串,n-比較的長度
所屬文件:string.h,mem.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
char*buf1="ABCDE123";
char*buf2="abcde456";
intstat;
stat=memcmp(buf1,buf2,5);
printf("Thestringstoposition5are");
if(stat)printf("not");
printf("thesame\n");
return0;
}
@函數(shù)名稱:memicmp
函數(shù)原型:int memicmp(const void *s1, const void *s2, size_t n)
函數(shù)功能:按字典順序、不考慮字母大小寫對字符串s1,s2前n個字符比較
函數(shù)返回:0,=0,0分別表示s1,=,s2
參數(shù)說明:s1,s2-要比較的字符串,n-比較的長度
所屬文件:string.h,mem.h
[cpp] view plain
#includestdio.h
#includestring.h
intmain()
{
char*buf1="ABCDE123";
char*buf2="abcde456";
intstat;
stat=memicmp(buf1,buf2,5);
printf("Thestringstoposition5are");
if(stat)printf("not");
printf("thesame");
return0;
}
輸出:
[cpp] view plain
/**************************************
Thestringstoposition5arethesame
***************************************/
@函數(shù)名稱:memmove
函數(shù)原型:void *memmove(void *dest, const void *src, size_t n)
函數(shù)功能:字符串拷貝
函數(shù)返回:指向dest的指針
參數(shù)說明:src-源字符串,n-拷貝的最大長度
所屬文件:string.h,mem.h
[cpp] view plain
#includestring.h
#includestdio.h
intmain()
{
chardest[40]="abcdefghijklmnopqrstuvwxyz0123456789";
printf("destinationpriortomemmove:%s\n",dest);
memmove(dest+1,dest,35);
printf("destinationaftermemmove:%s",dest);
return0;
}
PS:與memcpy不同的是,memmove可以處理目的字符串與源字符串地址空間出現(xiàn)重疊的情況,可保證待復制的內容不被破壞。
@函數(shù)名稱: memset
函數(shù)原型: void *memset(void *s, int c, size_t n)
函數(shù)功能: 字符串中的n個字節(jié)內容設置為c
函數(shù)返回:
參數(shù)說明: s-要設置的字符串,c-設置的內容,n-長度
所屬文件: string.h,mem.h
[cpp] view plain
#includestring.h
#includestdio.h
#includemem.h
intmain()
{
charbuffer[]="Helloworld";
printf("Bufferbeforememset:%s/n",buffer);
memset(buffer,'*',strlen(buffer)-1);
printf("Bufferaftermemset:%s",buffer);
return0;
}
c語言string的用法大全相關 文章 :
★ c語言string的用法
★ c語言的用法
★ Linux C語言字符與字符串處理
★ c語言中strcmp的用法
★ c語言大括號的用法
★ c語言位運算符的用法
★ c語言char的用法
★ c語言中sort的用法詳解
★ c語言中int的用法
★ c語言map的用法
當前題目:c語言函數(shù)string c語言函數(shù)strcpy
網頁地址:http://www.rwnh.cn/article20/ddcpsjo.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站內鏈、標簽優(yōu)化、營銷型網站建設、品牌網站制作、網站建設、動態(tài)網站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)