basic_string& assign (const E*s); //直接使用字串賦值
basic_string& assign (const E*s, size_type n);
basic_string& assign (const basic_string & str, size_type pos, size_type n);
//將str的子串賦值給呼叫串
basic_string& assign (const basic_string& str); //使用字串的“參照”賦值
basic_string& assign (size_type n, E c) ; //使用 n個重複字元賦值
basic_string& assign (const_iterator first, const_iterator last); //使用疊代器賦值
#include <iostream> #include <string> using namespace std; int main() { string str1 ("123456"); string str; str.assign (str1); //直接賦值 cout << str << endl; str.assign (str1, 3, 3); //賦值給子串 cout << str << endl; str.assign (str1,2,str1.npos);//賦值給從位置 2 至末尾的子串 cout << str << endl; str.assign (5,'X'); //重複 5 個'X'字元 cout << str << endl; string::iterator itB; string::iterator itE; itB = str1.begin (); itE = str1.end(); str.assign (itB, (--itE)); //從第 1 個至倒數第 2 個元素,賦值給字串 str cout << str << endl; return 0; }
iterator erase (iterator first, iterator last);
iterator erase (iterator it);
basic_string& erase (size_type p0 = 0, size_type n = npos);
str.erase (str* begin(), str.end());
或 str.erase (3);
void swap (basic_string& str);
swap()函數的使用方法為:
string str2 ("abcdefghijklmn");
str.swap (str2);
basic_string& insert (size_type p0 , const E * s); //插人 1 個字元至字串 s 前面
basic_string& insert (size_type p0 , const E * s, size_type n); // 將 s 的前 3 個字元插入p0 位置
basic_string& insert (size_type p0, const basic_string& str);
basic_string& insert (size_type p0, const basic_string& str,size_type pos, size_type n); //選取 str 的子串
basic_string& insert (size_type p0, size_type n, E c); //在下標 p0 位置插入 n 個字元 c
iterator insert (iterator it, E c); //在 it 位置插入字元 c
void insert (iterator it, const_iterator first, const_iterator last); //在字串前插入字元
void insert (iterator it, size_type n, E c) ; //在 it 位置重複插入 n 個字元 c
string A("ello"); string B ; B.insert(1,A); cout << B << endl; A = "ello"; B = "H"; B.insert (1,"yanchy ",3); cout<< B <<endl; A = "ello"; B = "H"; B.insert (1,A,2,2); cout << B << endl; A="ello"; B.insert (1 , 5 , 'C'); cout << B << endl; A = "ello"; string::iterator it = B.begin () +1; const string:: iterator itF = A.begin(); const string:: iterator itG = A.end(); B.insert(it,itF,itG); cout << B << endl;
basic_string& append (const E * s); //在原始字串後面追加字串s
basic_string& append (const E * s, size_type n);//在原始字串後面追加字串 s 的前 n 個字元
basic_string& append (const basic_string& str, size_type pos,size_type n);//在原始字串後面追加字串 s 的子串 s [ pos,…,pos +n -1]
basic_string& append (const basic_string& str);
basic_string& append (size_type n, E c); //追加 n 個重複字元
basic_string& append (const_iterator first, const_iterator last); //使用疊代器追加
A = "ello"; cout << A << endl; cout << B << endl; B.append(A); cout << B << endl; A = "ello"; cout << A << endl; cout << B << endl; B.append("12345",2); cout << B << endl; A = "ello"; cout << A << endl; cout << B << endl; B.append("12345",2,3); cout << B << endl; A = "ello"; B = "H"; cout << A << endl; cout << B << endl; B.append (10, 'a'); cout << B << endl; A = "ello"; B = 'H'; cout << A << endl ; cout << B << endl; B.append(A.begin(), A, end()); cout << B << endl;下面通過一個完整的例子介紹這些函數的使用:
#include <iostream> #include <string> using namespace std; int main () { string str1 ("123456"); string str2 ("abcdefghijklmn"); string str; str.assign(str1); cout << str << endl; str.assign (str1 , 3, 3); cout << str << endl; str.assign (str1, 2, str1.npos); cout << str << endl; str.assign (5, 'X'); cout << str << endl; string::iterator itB; string::iterator itE; itB = str1.begin (); itE = str1.end(); str.assign (itB, (--itE)); cout << str << endl; str = str1; cout << str << endl; str.erase(3); cout << str << endl; str.erase (str.begin (), str.end()); cout << ":" << str << ":" << endl; str.swap(str2); cout << str << endl; string A ("ello"); string B ("H"); B.insert (1, A); cout << B << endl; A = "ello"; B ='H'; B.insert (1, "yanchy ", 3); cout << "插入: " << B << endl; A = "ello"; B = "H"; B.insert(1,A,2,2); cout << "插入:" << B << endl; A = "ello"; B = "H"; B.insert (1,5,'C'); cout << "插入:" << B << endl; A = "ello"; B = "H"; string::iterator it = B.begin () +1; const string::iterator itF = A.begin (); const string::iterator itG = A.end (); B.insert(it,itF,itG); cout<<"插入:"<< B << endl; A = "ello"; B = "H"; cout << "A = " << A <<", B = " << B << endl ; B.append (A); cout << "追加:" << B << endl; B = "H"; cout << "A = "<< A << ", B= " << B << endl; B.append("12345", 2); cout << "追加:" << B << endl; A = "ello"; B = "H"; cout << "A = " << A << ", B= " << B << endl; B.append ("12345", 2, 3); cout << "追加:" << B << endl; A = "ello"; B = "H"; cout << "A = " << A <<", B = " << B << endl; B.append (10 , 'a'); cout << "追加:"<< B << endl; A = "ello"; B = "H"; cout << "A = " << A << ", B = " << B << endl; B.append(A.begin() , A.end()); cout << "追加:" << B << endl; cin.get(); return 0; }程式執行結果:
123456
456
3456
XXXXX
12345
123456
123
::
abcdefghijklmn
Hello
插入: Hyan
插入:Hlo
插入:HCCCCC
插入:Hello
A = ello, B = H
追加:Hello
A = ello, B= H
追加:H12
A = ello, B= H
追加:H345
A = ello, B = H
追加:Haaaaaaaaaa
A = ello, B = H
追加:Hello
basic_string& replace (size_type p0, size_type n0, const E * s); //使用字串 s 中的 n 個字元,從源串的位置 P0 處開始替換
basic_string& replace (size_type p0, size_type n0, const E *s, size_type n); //使用字串 s 中的 n 個字元,從源串的位置 P0 處開始替換 1 個字元
basic_string& replace (size_type p0, size_type n0, const basic_string& str); //使用字串 s 中的 n 個字元,從源串的位置 P0 處開始替換
basic_string& replace (size_type p0, size_type n0, const basic_string& str, size_type pos, size_type n); //使用串 str 的子串 str [pos, pos + n-1] 替換源串中的內容,從位置 p0 處開始替換,替換字元 n0 個
basic_string& replace (size_type p0, size_type n0, size_type n, E c); //使用 n 個字元 'c' 替換源串中位置 p0 處開始的 n0 個字元
basic_string& replace (iterator first0, iterator last0, const E * s);//使用疊代器替換,和 1) 用法類似
basic_string& replace (iterator first0, iterator last0, const E * s, size_type n);//和 2) 類似
basic_string& replace (iterator first0, iterator last0, const basic_string& str); //和 3) 類似
basic_string& replace (iterator first0, iterator last0, size_type n, E c); //和 5) 類似
basic_string& replace (iterator first0, iterator last0, const_iterator first, const_iterator last); //使用疊代器替換
#include <iostream> #include <string> using namespace std; int main () { string var ("abcdefghijklmn"); const string dest ("1234"); string dest2 ("567891234"); var.replace (3,3, dest); cout << "1: " << var << endl; var = "abcdefghijklmn"; var.replace (3,1, dest.c_str(), 1, 3); cout << "2: " << var << endl; var ="abcdefghijklmn"; var.replace (3, 1, 5, 'x'); cout << "3: " << var << endl; string::iterator itA, itB; string::iterator itC, itD; itA = var.begin(); itB = var.end(); var = "abcdefghijklmn"; var.replace (itA, itB, dest); cout << "4: " << var << endl; itA = var.begin (); itB = var.end(); itC = dest2.begin () +1; itD = dest2.end (); var = "abodefghijklmn"; var.replace (itA, itB, itC, itD); cout << "5: " << var << endl; var = "abcdefghijklmn"; var.replace (3, 1, dest.c_str(), 4); //這種方式會限定字串替換的最大長度 cout <<"6: " << var << endl; return 0; }程式執行結果為:
1: abc1234ghijklmn
2: abc234efghijklmn
3: abcxxxxxefghijklmn
4: 1234
5: 67891234efghijklmn
6: abc1234efghijklmn