1. 程式人生 > >C++基本操作(一):string

C++基本操作(一):string

1,統計字串長度
   int length();
   
2, 判斷是否為空
   bool empty();
   
3, 字串的連線
   string &append(const string &s,int pos,int n);


4,字串的擷取
   string substr(int pos=0,int npos=n)//返回從pos開始的n個字元組成的字串
   
5,字串的查詢(只舉最簡單的例子)
   int find(const string &s, int pos = 0) const;//從pos開始查詢字串s在當前串中的位置
                                                //查詢成功時返回所在位置,失敗返回string::npos的值 
6,字串的替換
   string &replace(int p0, int n0,const string &s);//刪除從p0開始的n0個字元,然後在p0處插入串s  
   
7,字串的插入
    string &insert(int p0,const string &s);


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

9,讀取一行字串
   cin.getline();
  或者 getline(cin,str);


10, string 轉化成 int
   int x = atoi(s.c_str());


11, int 轉化成 string
   string s= to_string(x);