c++ string 删除字符_字符串操作的全面总结(附完整代码)
生活随笔
收集整理的這篇文章主要介紹了
c++ string 删除字符_字符串操作的全面总结(附完整代码)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
字符串操作看似簡單,其實非常重要,不注意的話,經常出現代碼運行結果和自己想要的不一致,甚至崩潰。本文總結了一些構建string對象方法、修改string對象的方法、string類型的操作函數、string類型的查找、string對象的比較。
代碼實例:#include
#include
using namespace std;
//20200527 測試字符串操作
int main()
{
string s1;
cout <
string s2(10,'f');
cout <
string s3(s2);
cout < //只是構造的時候拷貝s2,修改其中一個不會影響另一個,s3輸出位ffffffffff
string s4(s3.begin(),s3.begin()+(s3.size())/2); //定義s4用迭代器做參數,從第一個迭代器s3.begin()
cout <
char *cp = "Hello"; //最后有空字符/0
char c_array[] = "world!!!!"; //最后有空字符/0
char no_null[] = {'H','e','l','l','0'}; //最后沒有空字符/0,不算C語言字符串,只是字符數組
string ss1(cp);
cout <
string ss2(c_array,5);
cout <
string ss3(c_array+5,4);
cout <
//string ss4(no_null);//用字符數組為ss4賦值,因為找不到/0,不知道拷貝幾個會出錯
string ss5(no_null,2); //這次取2個就知道什么時候結束,不會出錯
cout <
s1 = "Hello";
cout << s1 << endl; //s1輸出Hello
string s6(s1,2);
cout << s6 << endl; //用s1初始化s6,2表示字符下標,從第二個字符開始到最后,s6為llo
string s7(s1,0,2);
cout << s7 << endl; //從s10開始取2個,s7為He
string s8(s1,0,8);
cout << s8 << endl; //從s1的第一個開始取8個,不夠8個就結束,s8為Hello
return 0;
}** 運行結果**:
代碼實例:#include
#include
using namespace std;
//2020.05.27 測試字符串操作
int main()
{
string s("hello");
string s2("abcdef");
string::iterator p = s.begin(); //迭代器p
s.insert(p,'A'); //在迭代器p指向的s開始之前插入A
cout << s << endl; //s為Ahello
s.insert(p,3,'B'); //p指向返回的Ahello的A處,在A之前插入3個B
cout << s << endl; //s為BBBAhello
string::iterator b = s2.begin(); //迭代器b
string::iterator e = s2.end(); //迭代器e
//p = s.begin(); //p指向s
s.insert(p,b,e); //在p指向的s之前插入b和e迭代器范圍內的元素abcdef
cout << s << endl; //s為abcdefBBBAhello
s = "hello";
cout << s << endl; //s為hello
s.assign(b,e); //s所有的元素倍替換為b到e之間的元素,b與e之間為s2
cout << s << endl; //s為abcdef
s.assign(8,'K');
cout << s << endl; //s為KKKKKKKK
p = s2.begin(); //迭代器p指向s2的a
s2.erase(p); //刪除迭代器p指向的元素a
cout << s2 << endl; //s2為bcdef
p = s2.begin(); //a被刪除,p指向b
p++; //指向c
p++; //指向d
string::iterator p2 = s2.end(); //p2迭代器指向f
p2--; //指向e
s2.erase(p,p2); //刪除p指向的d和p2指向的e之間的元素
cout << s2 << endl; //s2為bcf
return 0;
}運行結果:運行結果string 類型特有的版本:string以數組的形式存儲,可以用數組的下標進行修改操作:string 修改操作方法說明
代碼實例:#include
#include
using namespace std;
//2020.05。27 測試字符串操作
int main()
{
string s("hello");
string s2("abc");
s.insert(0,3,'A'); //在s下標是0之前插入3個A
cout << s << endl; //s為AAAhello
s.insert(5,s2); //在AAAhello下標是5的元素之前插入abc
cout << s << endl; //s為AAAheabcllo
s2 = "123456";
s.insert(0,s2,2,3); //在s的下標是0之前插入s2下標為2開始往后的3個元素345
cout << s << endl; //s為345AAAheabcllo
char *cp = "Stately plup Buck";
s.assign (cp,7);
cout << s << endl; //s為Stately
s.assign(cp); //沒有長度,默認是拷貝全部
cout << s << endl; //s為Stately plup Buck
s = "hello";
s.insert (0,cp,7);
cout << s <
s.insert(0,cp);
cout << s <
s = "hello";
s2 = "abcdef";
s.assign(s2,2,3); //s2中下標為2開始3個元素賦值給s;
cout << s <
s.assign(s2);
cout << s <
s.erase (2,3); //從下標為2開始刪除s中的3個元素
cout << s <
s = "123456789";
s.erase(s.size()-5,5); //刪除s中后5個
cout << s <
s.insert(s.size(),5,'!'); //在s下標為s.size()處,插入5個!
cout << s <
s = "abc";
s.erase(0,1).insert(0,"A"); //先從下標為0之前刪除一個a為bc,再插入A
cout << s <
s = "abc";
s[0] = 'A'; //用數組的方式處理
cout << s <
return 0;
}運行結果:運行結果
#include
using namespace std;
//2020.05.27 測試字符串操作
int main()
{
string s("Hello world");
string s2 = s.substr(6,5); //從第6個開始取5個
cout << s2 << endl ; //s2為world
s2 = s.substr(6); //從第6個開始取拷貝所有的
cout << s2 << endl ; //s2為world
s2 = s.substr(6); //s2拷貝s的全部,相當于s2=s
cout << s2 << endl ; //s2為Hello world
s = "C++ Primer";
s.append(" 3rd Ed"); //再s最后添加3rd Ed
cout << s<< endl ; //s為C++ Primer 3rd Ed
s = "C++ Primer";
s.insert(s.size()," 3rd Ed"); //最后插入
cout << s<< endl ; //s為C++ Primer 3rd Ed
s.replace(11,3,"4th"); //下標11開始3個替換4th
cout << s<< endl ; //s為C++ Primer 4th Ed
s.replace(11,3,"Fourth"); //下標11開始3個替換Fourth
cout << s<< endl ; //s為C++ Primer Fourth Ed
s = "C++ Primer 3rd Ed"; //replace相當于先刪除后插入
s.erase (11,3); //刪除3rd
s.insert(11,"Fourth"); //插入Fourth
cout << s<< endl ; //s為C++ Primer Fourth Ed
return 0;
}運行結果:運行結果
代碼實例:#include
#include
using namespace std;
//2020.05.27 測試字符串操作
int main()
{
string name("AnnaBelle");
string::size_type pos1 = name.find("Bell");
cout << pos1 << endl; //返回下標4,如果沒找到返回npos
if(pos1 == string::npos)
cout << "沒找到!" << endl;
else
cout << "找到了!下標:" << pos1 <
name = "2sn3";
string numerics("0123456789");
string::size_type pos = name.find_first_of(numerics); //在2sn3中查找0123456789中任意一個第一次出現
if(pos == string::npos)
cout << "沒找到!" << endl;
else
cout << "找到了!下標:" << pos <
//其他類型的查找這里就不舉例子了
return 0;
}運行結果:運行結果
代碼實例:
#include
#include
#include
using std::cout;
using std::endl;
using std::cin;
using std::string;
int main(void)
{
string str1="hi,test,hello";
string str2="hi,test";
//字符串比較
if(str1.compare(str2)>0)
printf("str1>str2\n");
else if(str1.compare(str2)<0)
printf("str1 else
printf("str1==str2\n");
//str1的子串(從索引3開始,包含4個字符)與str2進行比較
if(str1.compare(3,4,str2)==0)
printf("str1的指定子串等于str2\n");
else
printf("str1的指定子串不等于str2\n");
//str1指定子串與str2的指定子串進行比較
if(str1.compare(3,4,str2,3,4)==0)
printf("str1的指定子串等于str2的指定子串\n");
else
printf("str1的指定子串不等于str2的指定子串\n");
//str1指定子串與字符串的前n個字符進行比較
if(str1.compare(0,2,"hi,hello",2)==0)
printf("str1的指定子串等于指定字符串的前2個字符組成的子串\n");
else
printf("str1的指定子串不等于指定字符串的前2個字符組成的子串\n");
return 0;
}運行結果:運行結果本文授權轉載自公眾號“C語言與CPP編程”,作者自成一派123
1 構建string對象方法
首先,為了在我們的程序中使用string類型,我們必須包含頭文件 。如下:#include 聲明一個字符串變量很簡單:string Str;這樣我們就聲明了一個字符串變量,但既然是一個類,就有構造函數和析構函數。上面的聲明沒有傳入參數,所以就直接使用了string的默認的構造函數,這個函數所作的就是把Str初始化為一個空字符串。String類的構造函數和析構函數如下:String類函數說明| string s; | 生成一個空字符串s |
| string s(s2); | 拷貝構造函數 生成s2的復制品 |
| string s("value"); | 用字符串value初始化s |
| string s(b,e); | 以區間b,e內的字符作為字符串s的初值 |
| string s(cp,n); | 取字符數組,前n個字符作初值 |
| string s(s2,pos2); | 將字符串s2"始于位置pos2"部分當作字符串的初值 |
| string s(s2,pos1,len); | 將字符串s2內"始于pos1且長度最多len"的部分作為字符串的初值 |
| s.~string(); | 銷毀所有字符,釋放內存 |
#include
using namespace std;
//20200527 測試字符串操作
int main()
{
string s1;
cout <
string s2(10,'f');
cout <
string s3(s2);
cout < //只是構造的時候拷貝s2,修改其中一個不會影響另一個,s3輸出位ffffffffff
string s4(s3.begin(),s3.begin()+(s3.size())/2); //定義s4用迭代器做參數,從第一個迭代器s3.begin()
cout <
char *cp = "Hello"; //最后有空字符/0
char c_array[] = "world!!!!"; //最后有空字符/0
char no_null[] = {'H','e','l','l','0'}; //最后沒有空字符/0,不算C語言字符串,只是字符數組
string ss1(cp);
cout <
string ss2(c_array,5);
cout <
string ss3(c_array+5,4);
cout <
//string ss4(no_null);//用字符數組為ss4賦值,因為找不到/0,不知道拷貝幾個會出錯
string ss5(no_null,2); //這次取2個就知道什么時候結束,不會出錯
cout <
s1 = "Hello";
cout << s1 << endl; //s1輸出Hello
string s6(s1,2);
cout << s6 << endl; //用s1初始化s6,2表示字符下標,從第二個字符開始到最后,s6為llo
string s7(s1,0,2);
cout << s7 << endl; //從s10開始取2個,s7為He
string s8(s1,0,8);
cout << s8 << endl; //從s1的第一個開始取8個,不夠8個就結束,s8為Hello
return 0;
}** 運行結果**:
2 修改string對象的方法
與容器共有的 string 操作:與容器共有的 string 操作方法說明| s.insert(p,t); | 在迭代器 p 指向的元素之前插入一個值為 t 的新元素,返回指向新插入元素的迭代器 |
| s.insert(p,n,t); | 在迭代器 p 指向的元素之前插入 n 個值為 t 的新元素 |
| s.insert(p,b,e); | 在迭代器 p 指向的元素之前插入迭代器 b 和 e 標記范圍內所有的元素。返回 void |
| s.assign(b,e); | 在迭代器 b 和 e 標記范圍內的元素替換 s。string類型,返回 s;容器類型返回 void |
| s.assign(n,t); | 用值為 t 的 n 個副本替換 s。對于 string 類型,該操作返回 s;對于容器類型,則返回 void |
| s.erase(p); | 刪除迭代器 p 指向的元素。返回一個迭代器,指向被 刪除元素后面的元素 |
| s.erase(b,e); | 刪除迭代器 b 和 e 標記范圍內所有的元素。返回一個迭代器,指向被刪除元素段后面的第一個元素 |
#include
using namespace std;
//2020.05.27 測試字符串操作
int main()
{
string s("hello");
string s2("abcdef");
string::iterator p = s.begin(); //迭代器p
s.insert(p,'A'); //在迭代器p指向的s開始之前插入A
cout << s << endl; //s為Ahello
s.insert(p,3,'B'); //p指向返回的Ahello的A處,在A之前插入3個B
cout << s << endl; //s為BBBAhello
string::iterator b = s2.begin(); //迭代器b
string::iterator e = s2.end(); //迭代器e
//p = s.begin(); //p指向s
s.insert(p,b,e); //在p指向的s之前插入b和e迭代器范圍內的元素abcdef
cout << s << endl; //s為abcdefBBBAhello
s = "hello";
cout << s << endl; //s為hello
s.assign(b,e); //s所有的元素倍替換為b到e之間的元素,b與e之間為s2
cout << s << endl; //s為abcdef
s.assign(8,'K');
cout << s << endl; //s為KKKKKKKK
p = s2.begin(); //迭代器p指向s2的a
s2.erase(p); //刪除迭代器p指向的元素a
cout << s2 << endl; //s2為bcdef
p = s2.begin(); //a被刪除,p指向b
p++; //指向c
p++; //指向d
string::iterator p2 = s2.end(); //p2迭代器指向f
p2--; //指向e
s2.erase(p,p2); //刪除p指向的d和p2指向的e之間的元素
cout << s2 << endl; //s2為bcf
return 0;
}運行結果:運行結果string 類型特有的版本:string以數組的形式存儲,可以用數組的下標進行修改操作:string 修改操作方法說明
| s.insert(pos,n,c); | 在下標 pos 的元素之前插入 n 個字符 c |
| s.insert(pos,s2); | 在下標 pos 的元素之前插入 string 對象 s2 |
| s.insert(pos,s2,pos2,len); | 在下標為 pos 的元素之前插入 s2 中從下標 ? pos2 開始的 len 個字符 |
| s.insert(pos,cp,len); | 在下標為 pos 打元素之前插入 cp 所指向數組的前len 個字符 |
| s.insert(pos,cp); | 在下標為 pos 的元素之前插入 cp 所指向的以空字符結束的字符串副本 |
| s.assign(s2); | 用 s2 的副本替換 s |
| s.assign(s2,pos2,len); | 用 s2 中從下標 pos2 開始的 len 個字符替換 s |
| s.assign(cp,len); | 用 cp 所指向數組的前 len 個字符副本替換 s |
| s.assign(cp); | 用 cp 所指向的以空字符結束的字符串替換 s |
| s.erase(pos,len); | 刪除從下標 pos 開始的 len 個字符 |
#include
using namespace std;
//2020.05。27 測試字符串操作
int main()
{
string s("hello");
string s2("abc");
s.insert(0,3,'A'); //在s下標是0之前插入3個A
cout << s << endl; //s為AAAhello
s.insert(5,s2); //在AAAhello下標是5的元素之前插入abc
cout << s << endl; //s為AAAheabcllo
s2 = "123456";
s.insert(0,s2,2,3); //在s的下標是0之前插入s2下標為2開始往后的3個元素345
cout << s << endl; //s為345AAAheabcllo
char *cp = "Stately plup Buck";
s.assign (cp,7);
cout << s << endl; //s為Stately
s.assign(cp); //沒有長度,默認是拷貝全部
cout << s << endl; //s為Stately plup Buck
s = "hello";
s.insert (0,cp,7);
cout << s <
s.insert(0,cp);
cout << s <
s = "hello";
s2 = "abcdef";
s.assign(s2,2,3); //s2中下標為2開始3個元素賦值給s;
cout << s <
s.assign(s2);
cout << s <
s.erase (2,3); //從下標為2開始刪除s中的3個元素
cout << s <
s = "123456789";
s.erase(s.size()-5,5); //刪除s中后5個
cout << s <
s.insert(s.size(),5,'!'); //在s下標為s.size()處,插入5個!
cout << s <
s = "abc";
s.erase(0,1).insert(0,"A"); //先從下標為0之前刪除一個a為bc,再插入A
cout << s <
s = "abc";
s[0] = 'A'; //用數組的方式處理
cout << s <
return 0;
}運行結果:運行結果
3 適合string類型操作的函數
- substr()主要功能是復制子字符串,要求從指定位置開始,并具有指定的長度。
- append() 方法在被選元素的結尾(仍然在內部)插入指定內容。提示:如需在被選元素的開頭插入內容,請使用prepend()方法。
- replace() 該函數返回一個字符串,其中指定的字符串已經被替換為另一字符串,并且替換的次數也可以指定。
#include
using namespace std;
//2020.05.27 測試字符串操作
int main()
{
string s("Hello world");
string s2 = s.substr(6,5); //從第6個開始取5個
cout << s2 << endl ; //s2為world
s2 = s.substr(6); //從第6個開始取拷貝所有的
cout << s2 << endl ; //s2為world
s2 = s.substr(6); //s2拷貝s的全部,相當于s2=s
cout << s2 << endl ; //s2為Hello world
s = "C++ Primer";
s.append(" 3rd Ed"); //再s最后添加3rd Ed
cout << s<< endl ; //s為C++ Primer 3rd Ed
s = "C++ Primer";
s.insert(s.size()," 3rd Ed"); //最后插入
cout << s<< endl ; //s為C++ Primer 3rd Ed
s.replace(11,3,"4th"); //下標11開始3個替換4th
cout << s<< endl ; //s為C++ Primer 4th Ed
s.replace(11,3,"Fourth"); //下標11開始3個替換Fourth
cout << s<< endl ; //s為C++ Primer Fourth Ed
s = "C++ Primer 3rd Ed"; //replace相當于先刪除后插入
s.erase (11,3); //刪除3rd
s.insert(11,"Fourth"); //插入Fourth
cout << s<< endl ; //s為C++ Primer Fourth Ed
return 0;
}運行結果:運行結果
4 string類型的查找
查找函數說明| s.find( args); | 在 s 中查找 args 的第一次出現 |
| s.rfind( args); | 在 s 中查找 args 的最后一次出現 |
| 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 的字符 |
#include
using namespace std;
//2020.05.27 測試字符串操作
int main()
{
string name("AnnaBelle");
string::size_type pos1 = name.find("Bell");
cout << pos1 << endl; //返回下標4,如果沒找到返回npos
if(pos1 == string::npos)
cout << "沒找到!" << endl;
else
cout << "找到了!下標:" << pos1 <
name = "2sn3";
string numerics("0123456789");
string::size_type pos = name.find_first_of(numerics); //在2sn3中查找0123456789中任意一個第一次出現
if(pos == string::npos)
cout << "沒找到!" << endl;
else
cout << "找到了!下標:" << pos <
//其他類型的查找這里就不舉例子了
return 0;
}運行結果:運行結果
5 string對象的比較
string對象比較函數compare用法說明| str1.compare(str2); | 如果相等則輸出為0,str1>str2輸出大于0,否則,輸出小于0 |
| str1.compare(m,n,str2); | str1的子串(從索引m開始,包含n個字符)與str2進行比較 |
| str1.compare(m,n,str2,m,n); | str1的子串(從索引m開始,包含n個字符)與str2的子串(從索引m開始,包含n個字符)進行比較 |
#include
#include
#include
using std::cout;
using std::endl;
using std::cin;
using std::string;
int main(void)
{
string str1="hi,test,hello";
string str2="hi,test";
//字符串比較
if(str1.compare(str2)>0)
printf("str1>str2\n");
else if(str1.compare(str2)<0)
printf("str1 else
printf("str1==str2\n");
//str1的子串(從索引3開始,包含4個字符)與str2進行比較
if(str1.compare(3,4,str2)==0)
printf("str1的指定子串等于str2\n");
else
printf("str1的指定子串不等于str2\n");
//str1指定子串與str2的指定子串進行比較
if(str1.compare(3,4,str2,3,4)==0)
printf("str1的指定子串等于str2的指定子串\n");
else
printf("str1的指定子串不等于str2的指定子串\n");
//str1指定子串與字符串的前n個字符進行比較
if(str1.compare(0,2,"hi,hello",2)==0)
printf("str1的指定子串等于指定字符串的前2個字符組成的子串\n");
else
printf("str1的指定子串不等于指定字符串的前2個字符組成的子串\n");
return 0;
}運行結果:運行結果本文授權轉載自公眾號“C語言與CPP編程”,作者自成一派123
-END-
推薦閱讀
【01】C語言十大經典排序算法(動態演示+代碼,值得收藏)【02】C語言、嵌入式中幾個非常實用的宏技巧【03】C語言最全入門筆記【04】絕對能檢測你C語言基礎水平的5個面試題【05】C語言為何不會過時?你需要掌握多少種語言?免責聲明:整理文章為傳播相關技術,版權歸原作者所有,如有侵權,請聯系刪除總結
以上是生活随笔為你收集整理的c++ string 删除字符_字符串操作的全面总结(附完整代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 减肥期间可以吃的水果
- 下一篇: 每天喝4000毫升水能减肥吗