當前位置:首頁 » 編程語言 » c語言string函數

c語言string函數

發布時間: 2024-04-11 04:16:47

1. c語言string的用法大全

C語言是一門面向過程的、抽象化的通用程序設計語言,廣泛應用於底層開發。C語言能以簡易的方式編譯、處理低級存儲器。C 語言string的用法有哪些呢,請看看下面我為你整理 總結 的c語言string的用法大全_C語言中string使用 方法 。

c語言string的用法

函數原型:char *strp(const char *s)

函數功能:字元串拷貝,目的空間由該函數分配

函數返回:指向拷貝後的字元串指針

參數說明:src-待拷貝的源字元串

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

#include<alloc.h>

intmain()

{

char*p_str,*string="abcde";

p_str=strp(string);

printf("%s",p_str);

free(p_str);

return0;

}

@函數名稱:strcpy

函數原型:char* strcpy(char* str1,char* str2);

函數功能:把str2指向的字元串拷貝到str1中去

函數返回:返回str1,即指向str1的指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[10];

char*str1="abcdefghi";

strcpy(string,str1);

printf("thestringis:%s ",string);

return0;

}

@函數名稱:strncpy

函數原型:char *strncpy(char *dest, const char *src,intcount)

函數功能:將字元串src中的count個字元拷貝到字元串dest中去

函數返回:指向dest的指針

參數說明:dest-目的字元串,src-源字元串,count-拷貝的字元個數

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.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只復制指定長度的字元,不會自動在末尾加''。若指定長度超過源字元串長度,不夠的部分補『』,

@函數名稱:strcat

函數原型:char* strcat(char * str1,char * str2);

函數功能:把字元串str2接到str1後面,str1最後的''被取消

函數返回:str1

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charbuffer[80];

strcpy(buffer,"Hello");

strcat(buffer,"world");

printf("%s ",buffer);

return0;

}

@函數名稱:strncat

函數原型:char *strncat(char *dest, const char *src, size_t maxlen)

函數功能:將字元串src中前maxlen個字元連接到dest中

函數返回:

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

charbuffer[80];

intmain()

{

strcpy(buffer,"Hello");

strncat(buffer,"world",8);

printf("%s ",buffer);

strncat(buffer,"*************",4);

printf("%s ",buffer);

return0;

}

注意:與strncpy不同的是,strncat會自動在末尾加『』,若指定長度超過源字元串長度,則只復制源字元串長度即停止

@函數名稱:strcmp

函數原型:int strcmp(char * str1,char * str2);

函數功能:比較兩個字元串str1,str2.

函數返回:str1<str2,返回負數;str1=str2,返回 0;str1>str2,返回正數.

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*buf1="aaa",*buf2="bbb",*buf3="ccc";

intptr;

ptr=strcmp(buf2,buf1);

if(ptr>0)

printf("buffer2isgreaterthanbuffer1 ");

else

printf("buffer2islessthanbuffer1 ");

ptr=strcmp(buf2,buf3);

if(ptr>0)

printf("buffer2isgreaterthanbuffer3 ");

else

printf("buffer2islessthanbuffer3 ");

return0;

}

@函數名稱:strncmp

函數原型:int strncmp(char *str1,char *str2,int count)

函數功能:對str1和str2中的前count個字元按字典順序比較

函數返回:小於0:str1<str2,等於0:str1=str2,大於0:str1>str2

參數說明:str1,str2-待比較的字元串,count-比較的長度

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

charstr1[]="aabbc";//

charstr2[]="abbcd";//

//為使測試程序更簡練,此處假定了strncmp只返回-1,0,1三個數

charres_info[]={'<','=','>'};

intres;

//前1個字元比較

res=strncmp(str1,str2,1);

printf("1:str1%cstr2 ",res_info[res+1]);

//前3個字元比較

res=strncmp(str1,str2,3);

printf("3:str1%cstr2 ",res_info[res+1]);

}

輸出:

[cpp] view plain

/****************************************

1:str1=str2

3:str1<str2

*****************************************/

@函數名稱:strpbrk

函數原型:char *strpbrk(const char *s1, const char *s2)

函數功能:得到s1中第一個「同時也出現在s2中」字元的位置指針

函數返回:位置指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*p="Findallvowels";

p=strpbrk(p+1,"aeiouAEIOU");

while(p)

{

printf("%s ",p);

p=strpbrk(p+1,"aeiouAEIOU");

}

return0;

}

輸出:

[cpp] view plain

/**************************************

indallvowels

allvowels

owels

els

**************************************/

@函數名稱:strcspn

函數原型:int strcspn(const char *s1, const char *s2)

函數功能:統計s1中從頭開始直到第一個「來自s2中的字元」出現的長度

函數返回:長度

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

printf("%d ",strcspn("abcbcadef","cba"));

printf("%d ",strcspn("xxxbcadef","cba"));

printf("%d ",strcspn("123456789","cba"));

return0;

}

輸出:

[cpp] view plain

/************************

0

3

9

************************/

@函數名稱:strspn

函數原型:int strspn(const char *s1, const char *s2)

函數功能:統計s1中從頭開始直到第一個「不來自s2中的字元」出現的長度

函數返回:位置指針

參數說明:

所屬文件:<string.h>

[html] view plain

#include<stdio.h>

#include<string.h>

#include<alloc.h>

intmain()

{

printf("%d ",strspn("abcbcadef","cba"));

printf("%d ",strspn("xxxbcadef","cba"));

printf("%d ",strspn("123456789","cba"));

return0;

}

輸出:

[cpp] view plain

/************************

6

0

0

************************/

@函數名稱:strchr

函數原型:char* strchr(char* str,char ch);

函數功能:找出str指向的字元串中第一次出現字元ch的位置

函數返回:返回指向該位置的指針,如找不到,則返回空指針

參數說明:str-待搜索的字元串,ch-查找的字元

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*str="Thisisastring!";

charch;

char*p;

while(1)

{

printf("Pleaseinputachar:");

ch=getchar();

p=strchr(str,ch);

if(p)

printf("%cisthe%dcharacterof"%s" ",ch,(int)(p-str+1),str);

else

printf("Notfound! ");

printf("PressESCtoquit! ");

if(27==getch())

break;

fflush(stdin);

}

return0;

}

運行結果:

[cpp] view plain

/********************************************

Pleaseinputachar:i

iisthe3characterof"Thisisastring!"

PressESCtoquit!

Pleaseinputachar:l

Notfound!

PressESCtoquit!

Pleaseinputachar:s

sisthe4characterof"Thisisastring!"

PressESCtoquit!

**********************************************/

@函數名稱:strrchr

函數原型:char *strrchr(const char *s, int c)

函數功能:得到字元串s中最後一個含有c字元的位置指針

函數返回:位置指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.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;

}

@函數名稱:strstr

函數原型:char* strstr(char* str1,char* str2);

函數功能:找出str2字元串在str1字元串中第一次出現的位置(不包括str2的串結束符)

函數返回:返回該位置的指針,如找不到,返回空指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*str1="OpenWatcomC/C++",*str2="Watcom",*ptr;

ptr=strstr(str1,str2);

printf("Thesubstringis:%s ",ptr);

return0;

}

輸出:

The substringis:Watcom C/C++

@函數名稱:strrev

函數原型:char *strrev(char *s)

函數功能:將字元串中的所有字元顛倒次序排列

函數返回:指向s的指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

charforward[]="string";//原文中定義為char*是不對的,指向代碼段的指針內容是不可變的

printf("Beforestrrev():%s",forward);

strrev(forward);

printf("Afterstrrev():%s",forward);

return0;

}

輸出:

[cpp] view plain

/************************************

Beforestrrev():string

Afterstrrev():gnirts

************************************/

@函數名稱:strnset

函數原型:char *strnset(char *s, int ch, size_t n)

函數功能:將字元串s中前n個字元設置為ch的值

函數返回:指向s的指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[]="aaaaaaaaaaaaaaaaaaaaaaa";

charletter='x';

printf("stringbeforestrnset:%s ",string);

strnset(string,letter,10);

printf("stringafterstrnset:%s ",string);

return0;

}

輸出:

[cpp] view plain

/*************************************************

stringbeforestrnset:aaaaaaaaaaaaaaaaaaaaaaa

stringafterstrnset:xxxxxxxxxxaaaaaaaaaaaaa

*************************************************/

@函數名稱:strset

函數原型:char *strset(char *s, int ch)

函數功能:將字元串s中所有字元設置為ch的值

函數返回:指向s的指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[10]="123456789";

charsymbol='c';

printf("Beforestrset():%s",string);

strset(string,symbol);

printf("Afterstrset():%s",string);

return0;

}

@函數名稱:strtok

函數原型:char *strtok(char *s1, const char *s2)

函數功能:分解s1字元串為用特定分隔符分隔的多個字元串(一般用於將英文句分解為單詞)

函數返回:字元串s1中首次出現s2中的字元前的子字元串指針

參數說明:s2一般設置為s1中的分隔字元

規定進行子調用時(即分割s1的第二、三及後續子串)第一參數必須是NULL

在每一次匹配成功後,將s1中分割出的子串位置替換為NULL(摘下鏈中第一個環),因此s1被破壞了

函數會記憶指針位置以供下一次調用

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*p;

char*buffer;

char*delims={".,"};

buffer=strp("Findwords,allofthem.");

printf("%s ",buffer);

p=strtok(buffer,delims);

while(p!=NULL){

printf("word:%s ",p);

p=strtok(NULL,delims);

}

printf("%s ",buffer);

return0;

}//根據測試,可以隨時給strtok的第一個參數輸入一個新的字元串,開始新字元串的分隔

PS:根據測試,可以隨時給strtok的第一個參數輸入一個新的字元串,開始新字元串的分隔

@函數名稱:strupr

函數原型:char *strupr(char *s)

函數功能:將字元串s中的字元變為大寫

函數返回:

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[]="abcdefghijklmnopqrstuvwxyz",*ptr;//會影響原字元串的內存,用char[]來聲明

ptr=strupr(string);

printf("%s",ptr);

return0;

}

@函數名稱:strlwr

函數原型:char *strlwr(char *s)

函數功能:將字元串中的字元變為小寫字元

函數返回:指向s的指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

intmain()

{

charstr[]="HOWTOSAY";

printf("%s",strlwr(str));

return0;

}

@函數名稱:strerror

函數原型:char *strerror(int errnum)

函數功能:得到錯誤信息的內容信息

函數返回:錯誤提示信息字元串指針

參數說明:errnum-錯誤編號

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<errno.h>

intmain()

{

char*buffer;

buffer=strerror(errno);

printf("Error:%s",buffer);

return0;

}

@函數名稱:memcpy

函數原型:void *memcpy(void *dest, const void *src, size_t n)

函數功能:字元串拷貝

函數返回:指向dest的指針

參數說明:src-源字元串,n-拷貝的最大長度

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charsrc[]="******************************";

chardest[]="";

char*ptr;

printf("destinationbeforememcpy:%s ",dest);

ptr=memcpy(dest,src,strlen(src));

if(ptr)

printf("destinationaftermemcpy:%s ",dest);

else

printf("memcpyfailed");

return0;

}

輸出:

[cpp] view plain

/*************************************************************

destinationbeforememcpy:

destinationaftermemcpy:******************************456709

**************************************************************/

@函數名稱:memccpy

函數原型:void *memccpy(void *dest, const void *src, int c, size_t n)

函數功能:字元串拷貝,到指定長度或遇到指定字元時停止拷貝

函數返回:

參數說明:src-源字元串指針,c-中止拷貝檢查字元,n-長度,dest-拷貝底目的字元串指針

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*src="Thisisthesourcestring";

chardest[50];

char*ptr;

ptr=memccpy(dest,src,'c',strlen(src));

if(ptr)

{

*ptr='';

printf("Thecharacterwasfound:%s",dest);

}

else

printf("Thecharacterwasn'tfound");

return0;

}

輸出:

[cpp] view plain

/*****************************************

Thecharacterwasfound:Thisisthesourc

*****************************************/

PS:指定字元被復制到dest中,memccpy返回了dest中指定字元的下一處的地址,返回NULL表示未遇到指定字元

@函數名稱:memchr

函數原型:void *memchr(const void *s, int c, size_t n)

函數功能:在字元串中第開始n個字元中尋找某個字元c的位置

函數返回:返回c的位置指針,返回NULL時表示未找到

參數說明:s-要搜索的字元串,c-要尋找的字元,n-指定長度

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.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;

}

@函數名稱:memcmp

函數原型:int memcmp(const void *s1, const void *s2,size_t n)

函數功能:按字典順序比較兩個串s1和s2的前n個位元組

函數返回:<0,=0,>0分別表示s1<,=,>s2

參數說明:s1,s2-要比較的字元串,n-比較的長度

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*buf1="ABCDE123";

char*buf2="abcde456";

intstat;

stat=memcmp(buf1,buf2,5);

printf("Thestringstoposition5are");

if(stat)printf("not");

printf("thesame ");

return0;

}

@函數名稱:memicmp

函數原型:int memicmp(const void *s1, const void *s2, size_t n)

函數功能:按字典順序、不考慮字母大小寫對字元串s1,s2前n個字元比較

函數返回:<0,=0,>0分別表示s1<,=,>s2

參數說明:s1,s2-要比較的字元串,n-比較的長度

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<stdio.h>

#include<string.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

/**************************************

***************************************/

@函數名稱:memmove

函數原型:void *memmove(void *dest, const void *src, size_t n)

函數功能:字元串拷貝

函數返回:指向dest的指針

參數說明:src-源字元串,n-拷貝的最大長度

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

chardest[40]="";

printf("destinationpriortomemmove:%s ",dest);

memmove(dest+1,dest,35);

printf("destinationaftermemmove:%s",dest);

return0;

}

PS:與memcpy不同的是,memmove可以處理目的字元串與源字元串地址空間出現重疊的情況,可保證待復制的內容不被破壞。

@函數名稱: memset

函數原型: void *memset(void *s, int c, size_t n)

函數功能: 字元串中的n個位元組內容設置為c

函數返回:

參數說明: s-要設置的字元串,c-設置的內容,n-長度

所屬文件: <string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

#include<mem.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的用法

2. c語言字元串處理函數的英文原名

函數名: strrchr
功 能: 在串中查找指定字元的最後一個出現
用 法: char *strrchr(char *str, char c);
舉例:
[cpp] view plain
char fullname="./lib/lib1.so";
char *ptr;
ptr = strrchr(fullname,'/');
printf("filename is %s",++ptr);
//運行結果:filename is lib1.so

函數名: strchr
功 能: 在串中查找指定字元的第一個出現
用 法: char *strchr(char *str, char c);
舉例:
[cpp] view plain
char fullname="./lib/lib1.so";
char *ptr;
ptr = strrchr(fullname,'.');
printf("after strchr() is %s",++ptr);
//運行結果:after strchr() is /lib/lib1.so

函數名: strtok
功 能: 在串中查找指定字元的第一個出現
用 法: char *strtok(char *s, char *delim);
說明:
1.strtok函數的實質上的處理是,strtok在s中查找包含在delim中的字元並用NULL(』/0′)來替換,直到找遍整個字元串。這句話有兩層含義:(1)每次調用strtok函數只能獲得一個分割單位。(2)要獲得所有的分割單元必須反復調用strtok函數。
2.strtok函數以後的調用時的需用NULL來替換s.
3.形參s(要分割的字元串)對應的變數應用char s[]=」….」形式,而不能用char *s=」….」形式。
舉例:
[cpp] view plain
void main()
{
char buf[]=」Golden Global View」;
char* token = strtok( buf, 」 「);
while( token != NULL )
{
printf( 」%s 「, token );
token = strtok( NULL, 」 「);
}
return 0;
}
/*其結果為:

Golden
Global
View
*/

3. C語言中string.h中用到的字元串處理函數有哪些

1.函數名: stpcpy
功 能: 拷貝一個字元串到另一個
2.函數名: strcat
功 能: 字元串拼接函數

3.函數名: strchr
功 能: 在一個串中查找給定字元的第一個匹配之處\

4.函數名: strcmp
功 能: 串比較

5.函數名: strncmpi
功 能: 將一個串中的一部分與另一個串比較, 不管大小寫

6.函數名: strcpy
功 能: 串拷貝

7.函數名: strcspn
功 能: 在串中查找第一個給定字元集內容的段

8.函數名: strp
功 能: 將串拷貝到新建的位置處

9.函數名: stricmp
功 能: 以大小寫不敏感方式比較兩個串

10.函數名: strerror
功 能: 返回指向錯誤信息字元串的指針
11函數名: strcmpi
功 能: 將一個串與另一個比較, 不管大小寫
12函數名: strncmp
功 能: 串比較
13函數名: strncmpi
功 能: 把串中的一部分與另一串中的一部分比較, 不管大小寫
14函數名: strncpy
功 能: 串拷貝
15函數名: strnicmp
功 能: 不注重大小寫地比較兩個串
16函數名: strnset
功 能: 將一個串中的所有字元都設為指定字元
17函數名: strpbrk
功 能: 在串中查找給定字元集中的字元
18函數名: strrchr
功 能: 在串中查找指定字元的最後一個出現
19函數名: strrev
功 能: 串倒轉
20函數名: strset
功 能: 將一個串中的所有字元都設為指定字元
21函數名: strspn
功 能: 在串中查找指定字元集的子集的第一次出現
22函數名: strstr
功 能: 在串中查找指定字元串的第一次出現
23函數名: strtod
功 能: 將字元串轉換為double型值
24函數名: strtok
功 能: 查找由在第二個串中指定的分界符分隔開的單詞
25函數名: strtol
功 能: 將串轉換為長整數
26函數名: strupr
功 能: 將串中的小寫字母轉換為大寫字母
27函數名: swab
功 能: 交換位元組

4. c語言中string怎麼用啊

用於輸入輸出的字元串函數, 在使用前應包含頭文件"stdio.h" ; 使用其它字元串函數則應包含頭文件"string.h"。

5. c語言中string是啥意思啊控制啥的啊

編程語言中的字元串,用雙引號引起來的幾個字元.如"Abc","一天"。String類是不可變(final)的,對String類的任何改變,都是返回一個新的String類對象。

這樣的話把String類的引用傳遞給一個方法,該方法對String的任何改變,對原引用指向的對象沒有任何影響,這一點和基本數據類型相似。

strings1,s2;

s1="abc";

s2=s1;

s2="def";

1、用法

string類的構造函數:

string(constchar*s);//用c字元串s初始化string(intn,charc);//用n個字元c初始化

此外,string類還支持默認構造函數和復制構造函數,如strings1;strings2="hello";都是正確的寫法。當構造的string太長而無法表達時會拋出length_error異常。

2、string類的字元操作:

constchar&operator[](intn)const;constchar&at(intn)const;char&operator[](intn);char&at(intn);

operator[]和at()均返回當前字元串中第n個字元的位置,但at函數提供范圍檢查,當越界時會拋出out_of_range異常,下標運算符[]不提供檢查訪問

constchar*data()const;//返回一個非null終止的c字元數組constchar*c_str()const;//返回一個以null終止的c字元串

int(char*s,intn,intpos=0)const;//把當前串中以pos開始的n個字元拷貝到以s為起始位置的字元數組中,返回實際拷貝的數目。

(5)c語言string函數擴展閱讀:

1、string的特性描述:

intcapacity()const;//返回當前容量(即string中不必增加內存即可存放的元素個數)

intmax_size()const;//返回string對象中可存放的最大字元串的長度

intsize()const;//返回當前字元串的大岩頃小

intlength()const;//返回當前字元串的長度

boolempty()const;//當前字元串是否為空

voidresize(intlen,charc);//把字元串當前大小置為len,並用字元c填充不足的部分

2、string類的輸入輸出操作:

string類重載運算符operator>>用於輸入,同樣重載運算符operator<<用於輸出操作。函數getline(istream&in,string&s);用於從輸入流in中讀取字元串到s中,以換行符' '分開。

3、string的賦值:

string&operator=(conststring&s);//把字元串s賦給當前字元串

string&assign(constchar*s);//用c類型字元串s賦值

string&assign(constchar*s,intn);//用c字元串s開始的n個字元賦值

string&assign(conststring&s);//把字元串s賦給當前字元串

string&assign(intn,charc);//用n個字元c賦值世配給當前字元串

string&assign(conststring&s,intstart,intn);//把字元粗返陸串s中從start開始的n個字元賦給當前字元

string&assign(const_iteratorfirst,const_itertorlast);//把first和last迭代器之間的部分賦給字元串

4、string的連接:

string&operator+=(conststring&s);//把字元串s連接到當前字元串的結尾

string&append(constchar*s);//把c類型字元串s連接到當前字元串結尾

string&append(constchar*s,intn);//把c類型字元串s的前n個字元連接到當前字元串結尾

string&append(conststring&s);//同operator+=()

string&append(conststring&s,intpos,intn);//把字元串s中從pos開始的n個字元連接到當前字元串的結尾

string&append(intn,charc);//在當前字元串結尾添加n個字元c

string&append(const_iteratorfirst,const_iteratorlast);//把迭代器first和last之間的部分連接到當前字元串的結尾

5、string的子串:

stringsubstr(intpos=0,intn=npos)const;//返回pos開始的n個字元組成的字元串

6、string的交換:

voidswap(string&s2);//交換當前字元串與s2的值

7、string類的查找函數:

intfind(charc,intpos=0)const;//從pos開始查找字元c在當前字元串的位置

intfind(constchar*s,intpos=0)const;//從pos開始查找字元串s在當前串中的位置

intfind(constchar*s,intpos,intn)const;//從pos開始查找字元串s中前n個字元在當前串中的位置

intfind(conststring&s,intpos=0)const;//從pos開始查找字元串s在當前串中的位置//查找成功時返回所在位置,失敗返回string::npos的值

intrfind(charc,intpos=npos)const;//從pos開始從後向前查找字元c在當前串中的位置

intrfind(constchar*s,intpos=npos)const;

intrfind(constchar*s,intpos,intn=npos)const;

intrfind(conststring&s,intpos=npos)const;//從pos開始從後向前查找字元串s中前n個字元組成的字元串在當前串中的位置,成功返回所在位置,失敗時返回string::npos的值

intfind_first_of(charc,intpos=0)const;//從pos開始查找字元c第一次出現的位置

intfind_first_of(constchar*s,intpos=0)const;

intfind_first_of(constchar*s,intpos,intn)const;

intfind_first_of(conststring&s,intpos=0)const;//從pos開始查找當前串中第一個在s的前n個字元組成的數組里的字元的位置。查找失敗返回string::npos

intfind_first_not_of(charc,intpos=0)const;

intfind_first_not_of(constchar*s,intpos=0)const;

intfind_first_not_of(constchar*s,intpos,intn)const;

intfind_first_not_of(conststring&s,intpos=0)const;//從當前串中查找第一個不在串s中的字元出現的位置,失敗返回string::npos

intfind_last_of(charc,intpos=npos)const;

intfind_last_of(constchar*s,intpos=npos)const;

intfind_last_of(constchar*s,intpos,intn=npos)const;

intfind_last_of(conststring&s,intpos=npos)const;

intfind_last_not_of(charc,intpos=npos)const;

intfind_last_not_of(constchar*s,intpos=npos)const;

intfind_last_not_of(constchar*s,intpos,intn)const;

intfind_last_not_of(conststring&s,intpos=npos)const;//find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,只不過是從後向前查找。

string類的替換函數:

string&replace(intp0,intn0,constchar*s);//刪除從p0開始的n0個字元,然後在p0處插入串s

string&replace(intp0,intn0,constchar*s,intn);//刪除p0開始的n0個字元,然後在p0處插入字元串s的前n個字元

string&replace(intp0,intn0,conststring&s);//刪除從p0開始的n0個字元,然後在p0處插入串s

string&replace(intp0,intn0,conststring&s,intpos,intn);//刪除p0開始的n0個字元,然後在p0處插入串s中從pos開始的n個字元

string&replace(intp0,intn0,intn,charc);//刪除p0開始的n0個字元,然後在p0處插入n個字元c

string&replace(iteratorfirst0,iteratorlast0,constchar*s);//把[first0,last0)之間的部分替換為字元串s

string&replace(iteratorfirst0,iteratorlast0,constchar*s,intn);//把[first0,last0)之間的部分替換為s的前n個字元。

string&replace(iteratorfirst0,iteratorlast0,conststring&s);//把[first0,last0)之間的部分替換為串s

string&replace(iteratorfirst0,iteratorlast0,intn,charc);//把[first0,last0)之間的部分替換為n個字元c

string&replace(iteratorfirst0,iteratorlast0,const_iteratorfirst,const_iteratorlast);//把[first0,last0)之間的部分替換成[first,last)之間的字元串。

string類的插入函數:

string&insert(intp0,constchar*s);

string&insert(intp0,constchar*s,intn);

string&insert(intp0,conststring&s);

string&insert(intp0,conststring&s,intpos,intn);//前4個函數在p0位置插入字元串s中pos開始的前n個字元

string&insert(intp0,intn,charc);//此函數在p0處插入n個字元c

iteratorinsert(iteratorit,charc);//在it處插入字元c,返回插入後迭代器的位置

voidinsert(iteratorit,const_iteratorfirst,const_iteratorlast);//在it處插入[first,last)之間的字元

voidinsert(iteratorit,intn,charc);//在it處插入n個字元c

string類的刪除函數

iteratorerase(iteratorfirst,iteratorlast);//刪除[first,last)之間的所有字元,返回刪除後迭代器的位置。

iteratorerase(iteratorit);//刪除it指向的字元,返回刪除後迭代器的位置。

string&erase(intpos=0,intn=npos);//刪除pos開始的n個字元,返回修改後的字元串。

string類的迭代器處理:

string類提供了向前和向後遍歷的迭代器iterator,迭代器提供了訪問各個字元的語法,類似於指針操作,迭代器不檢查范圍。

用string::iterator或string::const_iterator聲明迭代器變數,const_iterator不允許改變迭代的內容。常用迭代器函數有:

const_iteratorbegin()const;iteratorbegin();//返回string的起始位置

const_iteratorend()const;iteratorend();//返回string的最後一個字元後面的位置

const_iteratorrbegin()const;iteratorrbegin();//返回string的最後一個字元的位置

const_iteratorrend()const;iteratorrend();//返回string第一個字元位置的前面

rbegin和rend用於從後向前的迭代訪問,通過設置迭代器string::reverse_iterator或string::const_reverse_iterator實現

字元串流處理:

通過定義ostringstream和istringstream變數實現,在#include<sstream>頭文件中。

例如:

stringinput("hello,thisisatest");

istringstreamis(input);

strings1,s2,s3,s4;

is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"

ostringstreamos;

os<<s1<<s2<<s3<<s4;

cout<<os.str();

熱點內容
vs添加編譯選項 發布:2024-04-30 01:06:10 瀏覽:613
編程紅碼 發布:2024-04-30 01:04:49 瀏覽:910
給數組賦值java 發布:2024-04-30 01:04:37 瀏覽:498
我的世界jave版如何開伺服器 發布:2024-04-30 01:02:34 瀏覽:901
safari清除緩存ipad 發布:2024-04-30 00:47:24 瀏覽:523
欄位級數據加密 發布:2024-04-30 00:34:59 瀏覽:73
編譯原理上機實驗構建預測分析器 發布:2024-04-30 00:05:47 瀏覽:571
安卓手機的應用商店是哪個 發布:2024-04-29 23:50:18 瀏覽:411
pythonbinascii 發布:2024-04-29 23:23:00 瀏覽:39
安卓怎麼讓運存變大 發布:2024-04-29 23:21:53 瀏覽:459