1. 程式人生 > >【String】 常用庫函式系列一(替換(replace),刪除(erase),取子串(substr) )

【String】 常用庫函式系列一(替換(replace),刪除(erase),取子串(substr) )

導航

String:: replace

string (1)  
string& replace (size_t pos,  size_t len,  const string& str);
string& replace (iterator i1, iterator i2, const string& str);
substring (2)   
string& replace (size_t pos,  size_t len,  const string& str,
                 size_t subpos, size_t sublen);
c-string
(3) string& replace (size_t pos, size_t len, const char* s); string& replace (iterator i1, iterator i2, const char* s); buffer (4) string& replace (size_t pos, size_t len, const char* s, size_t n); string& replace (iterator i1, iterator i2, const char* s, size_t n); fill (5
) string& replace (size_t pos, size_t len, size_t n, char c); string& replace (iterator i1, iterator i2, size_t n, char c); range (6) template <class InputIterator> string& replace (iterator i1, iterator i2, InputIterator first, InputIterator last);

引數介紹:
str : 用來替換的字串
pos:原字串替換的第一個字元的位置
len: 替換子串的長度
subpos: 用來替換的字串的起始位置
sublen:用來替換的字串的長度
s: char型別陣列的首地址
n: 替換字元的數量
c: 用來替換的字元
first: 原字串替換的初始位置
last:原字串替換的末位置 (重點提示,[first,last) 這是左閉右開的)

如果對上面的引數還比較模糊,可以參考下面程式碼:

舉例:

// replacing in a string
#include <iostream>
#include <string>

int main ()
{
  std::string base="this is a test string.";
  std::string str2="n example";
  std::string str3="sample phrase";
  std::string str4="useful.";

  // replace signatures used in the same order as described above:

  // Using positions:                 0123456789*123456789*12345
  std::string str=base;           // "this is a test string."
  str.replace(9,5,str2);          // "this is an example string." (1)
  str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
  str.replace(8,10,"just a");     // "this is just a phrase."     (3)
  str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4)
  str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5)

  // Using iterators:                                               0123456789*123456789*
  str.replace(str.begin(),str.end()-3,str3);                    // "sample phrase!!!"      (1)
  str.replace(str.begin(),str.begin()+6,"replace");             // "replace phrase!!!"     (3)
  str.replace(str.begin()+8,str.begin()+14,"is coolness",7);    // "replace is cool!!!"    (4)
  str.replace(str.begin()+12,str.end()-4,4,'o');                // "replace is cooool!!!"  (5)
  str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful."    (6)
  std::cout << str << '\n';
  return 0;
}

String::erase

sequence (1)    
string& erase (size_t pos = 0, size_t len = npos);
character (2)   
iterator erase (iterator p);
range (3)   
iterator erase (iterator first, iterator last);

引數說明:
pos:待刪除的第一個字元的位置
len:待刪除字串的長度
p:待刪除字元的迭代器指向的位置
first,last:待刪除子串的迭代器起始和結束位置 (也是左閉右開)

舉例:

// string::erase
#include <iostream>
#include <string>

int main ()
{
  std::string str ("This is an example sentence.");
  std::cout << str << '\n';
                                           // "This is an example sentence."
  str.erase (10,8);                        //            ^^^^^^^^
  std::cout << str << '\n';
                                           // "This is an sentence."
  str.erase (str.begin()+9);               //           ^
  std::cout << str << '\n';
                                           // "This is a sentence."
  str.erase (str.begin()+5, str.end()-9);  //       ^^^^^
  std::cout << str << '\n';
                                           // "This sentence."
  return 0;
}

【OUTPUT】
This is an example sentence.
This is an sentence.
This is a sentence.
This sentence.

String::substr

string substr (size_t pos = 0, size_t len = npos) const;

引數說明:

Pos :要複製的子串的第一個字元的位置,不能超過原字串長度
Len:要複製的子串的長度,如果不寫,則從Pos位置一直到原字串的結尾處

舉例:

// string::substr
#include <iostream>
#include <string>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (3,5);     // "think"

  std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr (pos);     // get from "live" to the end

  std::cout << str2 << '\n' << str3 << '\n';

  return 0;
}

【OUTPUT】
think
live in details.