c++中string常用操作總結


前言:

string是C++標准庫最重要的類型之一,string支持長度可變的字符串,其包含在string頭文件中

#include  <string>  // 注意這里不是string.h,string.h是C字符串頭文件

(1)string常用初始化操作

//聲明string對象
    string s1;          
//初始化
    string s2 = "hello";         //使用字符串初始化
    string s3 = s2;              //用一個string對象初始化另一個string對象
    string s4(4, 'c');           //使用n個字符初始化
    string s5("hello", 2);       //將字符串的前n個字符賦值給s5
    string s6(s2, 2, 2);         //將s2從pos位置開始的len長度字符串賦值給s6
    string s7(s2, 2);            //將s2從pos位置開始的子串賦值給s7
    string s8(s2.begin(), s2.end()); //將s2某個區間的子串賦值給s8

(2)string增刪改查

//
    s1.insert(pos, "hello");        //從pos位置起,增加char *字符串
    s2.insert(pos, s1);             //從pos位置起,增加字符串
    s2.insert(pos, n, 'c');         //從pos位置開始,增加n個字符
    s2.insert(pos, s1, pos2, len);  //從pos位置起,插入s1的從pos2開始的len長度字符串
//
    s3.erase(pos, len);                    //刪除從pos位置開始的n個字符
    s3.erase(s3.begin()+1, s3.begin()+2);  //刪除某個指定區間的元素
//
    s4.replace(s4.begin(), s4.end(), "hello");    //替換指定區間內的字符
    s4.replace(pos, len, "shao");                 //替換從pos位置開始的len長度的字符串
//
    int find(const string&str, int pos =0) const;    //查找str第一次出現位置,從pos開始查找
    int find(const char *s, int pos =0) const;       //查找s第一次出現位置,從pos開始查找
    int find(const char *s, int pos, int n) const;   //從pos位置開始查找s的前n個字符第一次出現位置
    int find(const char c, int pos =0) const;        //查找字符c第一次出現位置
其他:
    int rfind()                      //逆序查找,即最后一次出現的位置
    s.find_first_of(args);            //在s中查找args的任意字符的第一次出現
    s.find_last_of(args);             //在s中查找args的任意字符的最后一次出現
    s.find_first_not_of(args);        //在s中查第一個不屬於args的字符
    s.find_last_not_of(args);         //在s中查找最后一一個不屬於args的字符

 (3)string存取操作

    s1.at(pos)   //通過at方法獲取pos位置的字符
    s1[pos]      //通過[]方法獲取pos位置的字符
    區別:at會拋出越界異常

(4)string拼接

          string對象的加法被定義為拼接,如把s2追加到s1的末尾可直接用s1+=s2表達式即可。當需要string對象和字符串混合拼接時,+操作符的左右操作數中必須有一個是string對象

string    s1="hello";
string    s2="world";
string    s3=s1+",";             //合法
string    s4="hello"+",";        //非法,+號兩邊都沒有string對象
string    s5=s1+","+"hello";     //合法,s1先和","拼接返回一個string對象,再和"hello"拼接
string    s6="hello"+","+s2;     //非法,"hello"先和","進行+操作,兩個都不是string對象
 常用方法:
String& operator += (const string& str);  //string + string類型
String& operator += (const char * str);   //string + char *類型
String& operator += (const char c);       //string + char 類型
String& append(const char *s);         //把字符串s連接到當前string的結尾
String& append(const char *s, int n);  //把字符串s的前n個字符連接到當前string結尾
String& append(const string &s);       //將string連接到當前string的結尾
String& append(const string &s, int pos, int n); //把string從pos開始的n個字符連接到當前string
String& append(int n, char c);                   //在當前string結束添加n個字符c

(5)string比較

          注意:compare函數在>時返回1,<時返回-1,==時返回0

                     比較區分大小寫,參考字典順序,排越前面的越小,如A比a小

    Int compare(const string &s) const;    //與string類型比較
    Int compare(const string &s) const;    //與char *類型比較
常用方法:

s.compare(s2);           //比較s和s2

s.compare(pos1,n1,s2);   //讓s中從pos1下標位置開始的n1個字符與s2做比較

s.compare(pos1,n1,s2,pos2,n2);  //讓s中從pos1下標位置開始的n1個字符與s2中從pos2開始的n2個字符相比較

(6)string子串

s.substr(pos,n); //返回一個string類型的字符串,它包含從s從下標pos開始的n個字符
s.substr(pos);   //返回一個string類型的字符串,它包含從下標pos開始到s的末尾的所有字符

(7)string和c-style字符串轉換

          C++提供的由C++字符串得到對應的C_string的方法是使用data()和c_str():

//string轉const char *
String str = “str to cstr”;
Const char * cstr = str.c_str();

//char * 轉string
Char * s = “cstr to str”;
String str(s);

注意:在C++中應堅持使用string類型進行運算,除非最后必須要轉化為char*,可以用函數strcpy實現 string str = "Hello World";
int len = str.length();
char *data = new char[len+1];
strcpy(data, str.c_str());  

其他方法:

s.assign()     // 賦以新值
swap()         // 交換兩個字符串的內容
s.clear()      // 刪除全部字符
==,!=,<,<=,>,>=,compare()  // 比較字符串
size(),length()  // 返回字符數量,兩者無區別
s.empty()        // 判斷字符串是否為空
reserve()        // 保留一定量內存以容納一定數量的字符
s.capacity()     // 返回重新分配之前的字符容量
copy()           // 將某值賦值為一個C_string
data()           // 將內容以字符數組形式返回,類型為const char *(在c++11后,等同於c_str())

 

 參考:

(1)https://www.baidu.com/link?url=SRFB1_BmZd74MUesBaVFHXvhVMfsO1IRZakze46h4BpavsOYZ5mh_rDQ-sWmgvoQmI_BdCdki9gSLS2EwUVict1hYpxZZGgFQS_YCxdHpiq&wd=&eqid=b48ebd1500151879000000035ff008ef

(2)https://www.cnblogs.com/maluning/p/8556862.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM