C++ string字符串的修改與替換方法詳析
字符串內(nèi)容的變化包括修改和替換兩種。本節(jié)將分別講解字符串內(nèi)容的修改和字符串內(nèi)容的替換。
字符串內(nèi)容的修改
可以通過使用多個函數(shù)修改字符串的值。例如 assign(),operator=,erase(),交換(swap),插入(insert)等。另外,還可通過 append() 函數(shù)添加字符。
下面逐一介紹各成員函數(shù)的使用方法。
assign()函數(shù)
使用 assign() 函數(shù)可以直接給字符串賦值。該函數(shù)既可以將整個字符串賦值給新串,也可以將字符串的子串賦值給新串。其在 basic_string 中的原型為:
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的子串賦值給調(diào)用串 basic_string& assign (const basic_string& str); //使用字符串的“引用”賦值 basic_string& assign (size_type n, E c) ; //使用 n個重復(fù)字符賦值 basic_string& assign (const_iterator first, const_iterator last); //使用迭代器賦值
以上幾種方法在例 1 中均有所體現(xiàn)。請讀者參考下述代碼。
【例 1】
#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'); //重復(fù) 5 個'X'字符 cout << str << endl; string::iterator itB; string::iterator itE; itB = str1.begin (); itE = str1.end(); str.assign (itB, (--itE)); //從第 1 個至倒數(shù)第 2 個元素,賦值給字符串 str cout << str << endl; return 0; }
operator= 函數(shù)
operator= 的功能就是賦值。
erase()函數(shù)
erase() 函數(shù)的原型為:
iterator erase (iterator first, iterator last); iterator erase (iterator it); basic_string& erase (size_type p0 = 0, size_type n = npos);
erase() 函數(shù)的使用方法為:
str.erase (str* begin(), str.end()); 或 str.erase (3);
swap()函數(shù)
swap()函數(shù)的原型為:
void swap (basic_string& str);
swap()函數(shù)的使用方法為:
string str2 ("abcdefghijklmn"); str.swap (str2);
insert()函數(shù)
insert () 函數(shù)的原型為:
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); //在下標(biāo) 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 位置重復(fù)插入 n 個字符 c
insert() 函數(shù)的使用方法為:
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;
append 函數(shù)
append() 函數(shù)的原型為:
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 個重復(fù)字符 basic_string& append (const_iterator first, const_iterator last); //使用迭代器追加 append() 函數(shù)的使用方法為: 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;
下面通過一個完整的例子介紹這些函數(shù)的使用:
#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; }
程序運行結(jié)果:
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
字符串內(nèi)容的替換
如果在一個字符串中標(biāo)識出具體位置,便可以通過下標(biāo)操作修改指定位置字符的值,或者替換某個子串。完成此項操作需要使用 string 類的成員函數(shù) replace()。
replace() 函數(shù)的原型如下: 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] 替換源串中的內(nèi)容,從位置 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); //使用迭代器替換
該函數(shù)的使用方法參照下面的程序:
#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; }
程序執(zhí)行結(jié)果為:
1: abc1234ghijklmn
2: abc234efghijklmn
3: abcxxxxxefghijklmn
4: 1234
5: 67891234efghijklmn
6: abc1234efghijklmn
本節(jié)講述了諸多可進行字符串內(nèi)容的修改和替換的函數(shù)及其使用方法,并給出了例題。由于每個函數(shù)可能有多個原型,希望讀者根據(jù)自己的情況,掌握其中的一種或兩種,以滿足自己使用的需要。同時,希望讀者能夠?qū)φ绽}的執(zhí)行效果,認(rèn)真閱讀本章節(jié)中的源代碼,徹底掌握本節(jié)內(nèi)容。
到此這篇關(guān)于C++ string字符串的修改與替換方法的文章就介紹到這了,更多相關(guān)C++ string字符串修改與替換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
va_list(),va_start(),va_arg(),va_end() 詳細解析
這些宏定義在stdarg.h中,所以用到可變參數(shù)的程序應(yīng)該包含這個頭文件.下面我們寫一個簡單的可變參數(shù)的函數(shù),該函數(shù)至少有一個整數(shù)參數(shù),第二個參數(shù)也是整數(shù),是可選的.函數(shù)只是打印這兩個參數(shù)的值2013-09-09簡要對比C語言中的truncate()函數(shù)與ftruncate()函數(shù)
這篇文章主要介紹了C語言中的truncate()函數(shù)與ftruncate()函數(shù)的簡要對比,注意其之間的區(qū)別,需要的朋友可以參考下2015-09-09C++利用std::forward_list查找插入數(shù)據(jù)方法示例
這篇文章主要給大家介紹了關(guān)于C++利用std::forward_list查找插入數(shù)據(jù)的相關(guān)資料,文中先對std::forward_list進行了詳細的介紹,而后通過示例代碼給大家介紹了查找的方法,需要的朋友可以參考借鑒,下面話不多說了,來一起看看吧。2017-08-08