C字串函式大全

2022-12-06 15:45:08 字數 1891 閱讀 1602

字串1、 字串的長度用length表示,string s=「hello!」; int l= 越界時會出現indexoutofrang……。

2、 複製字串 string string1=「how do you do!」; string string2=string1;

copy()函式。 string string2=

copyto()函式用於把字串的一部分複製到另乙個字元陣列。

int d=2,x=3,count=2;char [ ] d, str, x , count); d:在原字串中的起始位置。 str:

目標陣列。 x:在目標陣列中是起始位置。

count:複製字元數。 str=「***w **」。

3、 比較字串string1=」hello」; string2=」hello」; (1)可以用==直接比較兩個字串是否相等。 if(string1==string2)

(2)equals()函式當兩個字串相等時返回true,否則返回false。

if( 或者if(

compareto()函式 if( 如果string14、 驗證字串首尾 (1)startwith()函式用來判定字串是否以特定的子串開頭。

string[ ] strings=;

for(i=0;i<

endswith()函式……結尾輸出(started 和ended);

5、 定位字元或子串 (1)indexof()函式可以找到指定字元在字串中第一次出現的位置,如果找不到,返回-1。

string sent=」you love to hear youself dance」;

int in1= int in2= int1=1; int2=0;

還可以指定搜尋的起始 int in1= int in2=

in1=5; in2=17; 還可以指定搜尋的位置向後搜尋多少個字元。

int in1= int in2= in1=5; in2=-1;

(2)lastindexof()函式最後一次出現的位置,如果找不到返回-1。從後往前搜尋。

int in1= in1=18;

int in2=

(3)indexofany()函式可以同時搜尋多個指定字元,搜尋到任何乙個指定字串即停止。

char[ ] str=;

int in1=

int in1= in1=12; int in1= in1=12;

6、擷取子串substring()函式 string str= str=」hear youself dance」.

string str= str=」love」;

7、拆分字串split()函式 char[ ] str以空格為分隔符

string[ ] words=

foreach(string word in words

8、更改大小寫

(1)toupper()函式把小寫轉換為大寫字母。

string str=」ok」;

string str1=

(2)tolower()函式把小寫轉換為大寫;

9、修改字串insert()函式、replace()、remove string str=」tree」;

string s= //在位置1插入字串「h」

s=「three」;

string s= ; //用字元『d』替換『e 』

s=「trdd」;

string s= ; //刪除從位置3開始到末尾的所有字串

s=「tre」;

10、插入格式化變數

int i=60,j=360; string str= +\n------\n,j,i,i+j;

C語言字串函式大全

函式名 stpcpy 功能 拷貝乙個字串到另乙個 用法 char stpcpy char destin,char source 程式例 include include int main void 函式名 strcat 功能 字串拼接函式 用法 char strcat char destin,char...

C語言 字串函式大全和詳解

atof 將字串轉換成浮點型數 atoi 將字串轉換成整型數 atol 將字串轉換成長整型數 strtod 將字串轉換成浮點數 strtol 將字串轉換成長整型數 strtoul 將字串轉換成無符號長整型數 toascii 將整型數轉換成合法的ascii 碼字元 toupper 將小寫字母轉換成大寫...

C語言 字串函式大全和詳解

void memset void dest,int c,size t count 將dest前面count個字元置為字元c.返回dest的值.void memmove void dest,const void src,size t count 從src複製count位元組的字元到dest.如果src...