|
一、string基本概念: 1、本质 string是c++风格的字符串,而string本质上是一个类。 2、string和char *的区别 -char *是一个char类型的指针类型 -string是一个类,类内部封装了char*,是一个char型的容器。string类内部封装了很多成员函数,比如:查找find,拷贝copy,删除delete,替换replace,插入insert;string管理char所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责 二、string构造函数: 构造函数原型:
代码应用举例: #include <iostream>#include <string># \# O* F: x# s1 i) G' z using namespace std; void test() { string s1;8 K9 Z5 g t9 o- ^ 7 s0 c6 y1 _! K7 j+ q- @3 X const char *str = "txp";) I" j+ Y6 b& a' L7 v string s2(str); cout << "s2= "<<s2<<endl; ' i) _, i2 J% z9 N9 n7 F string s3(str);: S0 `4 \# S0 X' f8 @: J! I! W cout <<"s3= "<<s3<<endl;5 U! v9 p9 m# i3 d2 | 1 z r0 V) s, z8 U0 ]/ m# A string s4(10,'a');1 ]$ h! l) @6 v" Y; g& j7 g cout <<"s4= "<<s4<<endl; } int main() {2 B5 [% s+ H* W$ Q/ C3 u, M& R test(); } 输出结果: root@txp-virtual-machine:/home/txp/test2# ./a.out0 e) T" a2 z H2 N% d/ |3 f2 ys2= txp5 Y- d! n Z& t2 ^4 ~3 B+ Q: A- m1 Q s3= txp, r( E6 V& G: t7 [7 e6 \ s4= aaaaaaaaaa - N- p1 Y( t. R: t 三、string赋值操作: 实现string字符串赋值的函数类型:
代码应用: #include <iostream>#include <string>, I( g+ M9 z% [# ]2 y7 a using namespace std; void test()$ X0 I2 U" ?( B6 K; ~ {2 [* l/ M4 d. Q string str1; str1 = "txp";" S/ Z4 e( ?0 P cout<<"str1= "<<str1<<endl; string str2; str2=str1;# r1 }; @( H' f- y+ l$ Q cout<<"str2 = "<<str2<<endl; string str3; str3='a';5 m5 M1 N! \$ `1 d cout<<"str3= "<<str3<<endl; string str4;7 a) b' o( r5 x str4.assign("txp like cpp"); cout<<"str4= "<<str4<<endl;* p, s0 J9 W6 D& \* _0 ^ string str5;$ _- K+ O! k1 E2 T: V$ G+ T2 I. p str5.assign("txp like cpp",5); cout<<"str5= "<<str5<<endl; " ]' n$ ^1 }# `1 j3 V string str6; str6.assign(str5); cout<<"str6= "<<str6<<endl;# X5 h/ l7 \' L. d# x " E& E- q/ E' c* ?1 N% | string str7; str7.assign(10,'w'); cout<<"str7= "<<str7<<endl; } int main()# d$ n# A, g; h& D5 D, ] {8 A' |) y5 j, Q" p" L2 S2 r" v test(); }# ?/ g; r" g' Q% i# h$ Z 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outstr1= txp str2 = txp str3= a str4= txp like cpp. u# _ E7 k' f- z str5= txp l str6= txp l1 J6 {) B+ p' t, R, ^7 E str7= wwwwwwwwww 9 \* B: p# r4 m) E0 l' E 四、string字符串拼接: 实现字符串末尾拼接字符串函数原型:
代码应用: #include <iostream>#include <string>* T' P4 U2 w4 h' f' I, d0 l using namespace std; void test() {. K& [% a- |3 r5 Z2 a% ? string str1 = "txp"; str1 += "xiaoping";0 B' e4 ^: l. Y: ~ cout <<"str1= "<<str1<<endl;- C. }: ?/ `, Z str1+=":";& _% G; A/ x+ {$ h: u" p cout<<"str1= "<<str1<<endl;$ [- O- w$ S' N3 r * D3 b8 E9 A9 s/ T& ~ string str2 = "cpp";% E6 L) O3 r7 i, L str1+=str2; cout<<"str1= "<<str1<<endl;) h) t# L9 d) f$ z) k string str3 = "I"; str3.append("love");4 n/ `) h# m9 k6 I% N+ ]: r cout<<"str3= "<<str3<<endl;3 d5 y0 C( g$ O . ^. g2 C! E8 ?: w% [" { str3.append("game abcde",4); cout<<"str3= "<<str3<<endl; $ ?4 f: |, G8 G! K/ e( ~4 m str3.append(str2);7 k) ], E- U' K& ]& M3 K: X) j cout<<"str3= "<<str3<<endl; ! ~/ Q1 M9 I& X0 i% l str3.append(str2,0,3);: l) j! S9 m n% b6 K cout<<"str3= "<<str3<<endl; } int main()! q% p3 D) X# t6 V6 E { test();, P7 k: v& d- u1 J3 Q& R) t# Q1 l; W }) J% [0 \3 Z, a7 m. x) h 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out+ N3 ~) ^& L* @: T5 n, {str1= txpxiaoping str1= txpxiaoping:8 [5 y J5 [ F str1= txpxiaoping:cpp! P5 {) m- H5 ]1 u+ N str3= Ilove str3= Ilovegame str3= Ilovegamecpp1 R4 l) _, m9 V% c str3= Ilovegamecppcpp 五、string查找和替换: 实现字符串的查找和替换函数原型:
代码应用: #include <iostream>#include <string> using namespace std; * y0 W3 G( A% _8 w1 o" r5 Z; M+ E1 V void test() {$ J5 R8 ~: J& V Z* { string str1 = "abcd"; int pos = str1.find("bc"); if(pos) { cout<< " find the word"<<endl; }+ n% |8 j+ p, b7 ^" s$ l else( Y+ W9 n2 o# s- q |1 Y) d { cout<<" not find the word"<<endl;, r4 z( Z7 p8 C0 m P3 n } cout<<"pos= "<<pos<<endl; //rfind和find的区别:rfind从右往左查找,find从左往右查找2 O0 C- I; `0 m8 C+ r2 D pos = str1.rfind("cd");: v8 N! F* g' F6 W6 r) s0 n. Y cout<<"pos= "<<pos<<endl;0 A- U7 H! ` [7 U! n5 { }+ Z3 A3 A1 x y& ?$ X void test1()& d# M: x9 C1 N {( x4 R- p [; O0 p string str2 = "abcd"; str2.replace(1,3,"1111");* u4 r# g3 e1 [+ a) I; N cout<<"str2= "<<str2<<endl; ( o7 Y% k3 h; e0 _' K } int main()2 B7 _! Y& o+ N% g {5 q& g4 V* h7 h3 H test(); test1();9 c$ Y) M3 y( l) W; X } 4 \: \) b! B$ b& `! T B! ]* | 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outfind the word pos= 1 pos= 2 str2= a1111 % t6 `# F- @. C" y1 z! ~ 六、string字符串比较: 比较方式:字符串比较是按字符的ASCII码进行对比的: = : 返回 0 > < : 返回 -1 函数原型:
代码应用: #include <iostream>#include <string>! }8 u6 k* v$ M3 o& ]3 i- g using namespace std; 8 Z& C" [& [3 b) P' U9 h- v6 X void test() { string str1 = "txp";& ~7 I: n2 y: f) l3 Y% g. z4 B, B3 ? string str2 = "linux"; n& a/ V( C3 A 3 s. `7 A8 g' g: ^, r4 Y' O if(str1.compare(str2)==0) {' w ]& `2 D" r- x8 ?' R( i cout<<"str1 = str2 "<<endl;: w) r- n! Z% o/ {* t* |$ y }" N& f# t. u0 p/ X* F/ W' m else if(str1.compare(str2)>0) { cout<<"str1 > str2 "<<endl;: h8 Q2 i& `4 R" |+ B$ w$ X }" E8 Z; Z5 L8 k0 _ else. C& h6 C; z. P1 V { cout<<" str1 < str2 "<<endl;/ U) |; a" `. ` }0 h! t/ c% N! T$ O } . n. Y5 Y. s0 ^. M/ v int main()( f5 f# K( W7 D8 W { test();4 t" K1 |7 O: E& R4 B2 ` D- k8 R6 n } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out7 N( c6 |# O, Q! s0 T& a' Gstr1 > str2 七、string字符存取: 实现字符存取函数原型:
代码应用: #include <iostream>& P& u& l1 _* ~( g#include <string>0 q7 u: N& V: j% g; U6 Z using namespace std; void test()5 _2 {' O8 | i& A4 M {4 D$ q+ `, |! w' s4 g* E' x' B K string str = "linux"; //通过[]访问单个字符 for(int i = 0;i< str.size();i++) {, n8 V2 m% Y4 }2 X( q' U5 y cout<<str<<" "; } cout<<endl;1 U% ^! K4 L- E. ~) F //通过at方式访问单个字符 for(int i =0; i<str.size();i++) {. w, r, E! f0 f, A) [* f cout<<str.at(i)<<" "; } cout<<endl;0 O% _. h1 H% _! N% G: L //修改单个字符的方式 str[0] = 'x';/ q- b. X. {4 ]5 v cout<<"str = "<<str<<endl; str.at(1) = 'x'; cout<<"str = "<<str<<endl; }( Q, w9 z* u: w$ o6 A; d , [7 v9 U8 f9 u9 u# f Q$ X int main() {: J7 g: X$ N- C$ I* } test(); * U9 Y# z7 X/ c6 D# I. { }4 d" H$ r8 @4 w7 c* J 输出结果: root@txp-virtual-machine:/home/txp/test2# ./a.out! _# }5 C" A: E) C" M2 tl i n u x l i n u x 1 Q6 F- j/ m' n; C str = xinux) v& j, z( }* }! a* k str = xxnux2 Y, T5 \/ K" W. g8 l6 J 八、string插入和删除: 实现string插入和删除的函数原型:
代码应用: #include <iostream>#include <string> using namespace std; void test()' g' E, H l+ f- f" ^/ ]7 t {* e5 C+ A2 P( V( ] string str = "linux"; ( b' m+ `9 {, Y$ f9 x9 d str.insert(1,"111"); cout<<"str = "<<str<<endl;9 L3 S+ y' x3 Y; Z W2 [4 W6 |. `) a str.erase(1,3); cout<<"str = "<<str<<endl; } 9 m2 H+ x" k1 S1 o& Z int main()9 G0 L& M- Z2 [ W% E { test(); / Z) O9 b+ I* z" k7 p } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outstr = l111inux str = linux4 O V( d$ h* i" Y* W! ^ 1 D( f8 p) X7 W4 k" s+ h$ v 九:string子串 实现从string字符串中获得子串的函数原型:
代码应用: #include <iostream>0 g& {, F( G7 |. d5 i% Y' L2 q" i#include <string> using namespace std;; D# Z' p4 D) B( n1 Z+ [4 O1 s 7 `; T* Q& R" [" t( ` G void test() {9 A# V! ~+ d% @ string str = "linux"; , b4 p- r- {9 p- S! w1 | string substr = str.substr(1,3); cout<<"substr = "<<substr<<endl; int pos = str.find('u');/ L+ k p- l( w! N8 p5 ? string ur = str.substr(0,pos);( u' U6 O- _( M! m, ?7 R/ W% }0 _ cout<<ur<<endl; }6 N& U1 Y, \6 K6 T4 x ! O8 e& \( m% | i1 x/ e! E! N int main() { test(); 5 [, s5 Y- |- G1 Q } . \! G; c5 ~ Y U: C: g' x' m 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outsubstr = inu7 e; R) ?8 ? A& c% F C lin$ r7 L D0 Z+ m$ k& K |
微信公众号
手机版