C++中string資料類型

來源:互聯網
上載者:User

標籤:string的初始化   尋找   遍曆   刪除   替換   插入   

1、string

 string的初始化,在C++中字串是一種資料類型;

(1)、string的初始化,遍曆,字串串連

代碼如下:

#include<iostream>#include<string>#include<stdio.h>using namespace std;int main(void){  //string的初始化,在C++中字串是一種資料類型;    string s1 = "abcdefg";    string s2("abcdefg");    string s3(s2);    string s4 = s1;  //調用拷貝建構函式;    string s5(10, ‘a‘);//10個空間中的字元都是‘a‘;    s5 = s1;     cout<<"s3:"<<s3<<endl;    cout<<"s5:"<<s5<<endl;//string的遍曆,重載了[]操作符;    //1、數組方式遍曆[]    for(int i = 0; i < s1.length(); i++){        cout<<s1[i]<<" ";  //出現錯誤(下標越界),不會向外面剖出異常,引起程式的中斷;    }       cout<<endl;    //2、迭代器    string::iterator it;     for(it = s1.begin(); it != s1.end(); it++){        cout<<*it<<" ";    }    cout<<endl;    //3、函數at()遍曆    for(int i = 0; i < s1.length(); i++){        cout<<s1.at(i)<<" "; //會剖出異常,合理的解決下標越界;    }    cout<<endl;//字元指標和string的轉換    //此時,把s1====>char * 把記憶體首地址給露出來;    printf("s1:%s \n", s1.c_str());    //s1中的內容拷貝到buf中;    char buf[123] = {0};    s1.copy(buf, 2, 0);//n, pos;下標從0開始拷貝2個字元到buf中,不會是C風格的,注意自己加上0結束標誌;    cout<<buf<<endl;//string子符串的串連    s1 = s1 + s2; //直接+就表:字串的串連;    s1 += s2; //+=也是字串的串連;    s1.append(s4); //調用方法append()也是字串的串連;    cout<<s1<<endl;           return 0;}

運行結果:

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/8A/B0/wKiom1g3j7jTmJwMAABN4l3uyzU176.png-wh_500x0-wm_3-wmp_4-s_3916689983.png" title="QQ20161125091056.png" alt="wKiom1g3j7jTmJwMAABN4l3uyzU176.png-wh_50" />

(2)、string的尋找,替換

代碼如下:

#include<iostream>#include<string>#include<string.h>using namespace std;int main(void){//字串的尋找和替換    string s1 = "wbm hello wbm 111 wbm 222 wbm 333";    //1、第一次出現wbm的下標    int index = s1.find("wbm", 0);     cout<<"index :"<<index<<endl;    //2、求wbm每一次出現的數組下標    /*  int offindex = s1.find("wbm", 0);    while(offindex != -1){        cout<<"offindex :"<<offindex<<endl;        offindex += strlen("wbm");        offindex = s1.find("wbm", offindex);    }*/    //3、把小寫wbm換成大寫    int offindex = s1.find("wbm", 0);     while(offindex != -1){        cout<<"offindex :"<<offindex<<endl;        s1.replace(offindex, strlen("wbm"), "WBM"); //從下標offindex開始,刪除n個字元,替換為後面的字元;        offindex += strlen("wbm");        offindex = s1.find("wbm", offindex);    }    cout<<"s1:"<<s1<<endl;    string s3 = "aaa bbb ccc";    s3.replace(0, 3, "AAA");  //替換的函數;    cout<<"s3:"<<s3<<endl;    return 0;}

運行結果:

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/8A/B0/wKiom1g3kG3QGnASAABPDPBlRuo423.png-wh_500x0-wm_3-wmp_4-s_223576687.png" title="QQ20161125091418.png" alt="wKiom1g3kG3QGnASAABPDPBlRuo423.png-wh_50" />

(3)、區間的刪除和插入

代碼如下:

#include<iostream>#include<string>#include<algorithm>using namespace std;int main(void){//區間刪除和插入    string s1 = "hello1 hello2 hell03";    string::iterator it = find(s1.begin(), s1.end(), ‘l‘);    if(it != s1.end()){        s1.erase(it); //刪除演算法;    }       cout<<"s1 :"<<s1<<endl;    s1.erase(s1.begin(), s1.end()); //刪除從pos開始的n個字元;    cout<<"s1全部刪除:"<<s1<<endl;    cout<<"s1的長度:"<<s1.length()<<endl;    string s2 = "BBB";    s2.insert(0, "AAA");  //頭插法    s2.insert(s2.length(), "CCC");//尾插法    cout<<s2<<endl;    return 0;}

運行結果:

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/8A/B0/wKiom1g3kOWwM1EqAAA6yoij2To820.png-wh_500x0-wm_3-wmp_4-s_291370346.png" title="QQ20161125091618.png" alt="wKiom1g3kOWwM1EqAAA6yoij2To820.png-wh_50" />

(4)、string的大小寫轉換-->函數指標

代碼如下:

#include<iostream>#include<string>#include<algorithm>using namespace std;int main(void){    string s1 = "AAAbbb";    transform(s1.begin(), s1.end(), s1.begin(), 0, toupper);//toupper可以是:函數的入口地址,函數對象,    cout<<s1<<endl;    string s2 = "AAAbbb";    transform(s2.begin(), s2.end(), s2.begin(), 0, tolower);    cout<<s2<<endl;    return 0;}





本文出自 “wait0804” 部落格,請務必保留此出處http://wait0804.blog.51cto.com/11586096/1876460

C++中string資料類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.