C++中对字符串的操作

从百度文库转来的一篇文章:

字符串操作是一个不小的主题,在标准C++中,string字符串类成为一个标准,之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下的需要.
    下面我们首先从一些示例开始学习下string类的使用.

  1 1)
2 #include <string>
3 #include <iostream>
4 using namespace std;
5
6 void main()
7 {
8 string s("hehe");
9 string s1="abcd";
10 cout<<s<<” “<<s1<<endl;
11
12 }
13 2)
14 #include <string>
15 #include <iostream>
16 using namespace std;
17 void main()
18 {
19 char chs[] = "hehe";
20 string s(chs);
21 cout<<s<<endl;
22
23 }
24 3)
25 #include <string>
26 #include <iostream>
27 using namespace std;
28
29 void main()
30 {
31 char chs[] = "hehe";
32 string s(chs,1,3); //指定从chs的索引1开始,最后复制3个字节
33 cout<<s<<endl;
34
35 }
36 4)
37 #include <string>
38 #include <iostream>
39 using namespace std;
40
41 void main()
42 {
43 string s1("hehe");
44 string s2(s1);
45 cout<<s2<<endl;
46
47 }
48 5)
49 #include <string>
50 #include <iostream>
51 using namespace std;
52 void main()
53 {
54 string s1("hehe",2,3);
55 string s2(s1);
56 cout<<s2<<endl;
57
58 }
59 6)
60 #include <string>
61 #include <iostream>
62 using namespace std;
63
64 void main()
65 {
66 char chs[] = "hehe";
67 string s(chs,3); //将chs前3个字符作为初值构造
68 cout<<s<<endl;
69
70 }
71 7)
72 #include <string>
73 #include <iostream>
74 using namespace std;
75 void main()
76 {
77 string s(10,'k'); //分配10个字符,初值都是'k'
78 cout<<s<<endl;
79
80 }
81 //以上是string类实例的构造手段,都很简单.
82
83 9)
84 //赋新值
85 #include <string>
86 #include <iostream>
87 using namespace std;
88
89 void main()
90 {
91 string s(10,'k'); //分配10个字符,初值都是'k'
92 cout<<s<<endl;
93 s = "hehehehe";
94 cout<<s<<endl;
95 s.assign("kdje");
96 cout<<s<<endl;
97 s.assign("fkdhfkdfd",5); //重新分配指定字符串的前5的元素内容
98 cout<<s<<endl;
99
100 }
101 10)
102 //swap方法交换
103 #include <string>
104 #include <iostream>
105 using namespace std;
106
107 void main()
108 {
109 string s1 = "hehe";
110 string s2 = "gagaga";
111 cout<<"s1 : "<<s1<<endl;
112 cout<<"s2 : "<<s2<<endl;
113 s1.swap(s2);
114 cout<<"s1 : "<<s1<<endl;
115 cout<<"s2 : "<<s2<<endl;
116
117 }
118 11)
119 //+=,append(),push_back()在尾部添加字符
120 #include <string>
121 #include <iostream>
122 using namespace std;
123
124 void main()
125 {
126 string s = "hehe";
127 s += "gaga";
128 cout<<s<<endl;
129 s.append("嘿嘿"); //append()方法可以添加字符串
130 cout<<s<<endl;
131 s.push_back('k'); //push_back()方法只能添加一个字符...
132 cout<<s<<endl;
133
134 }
135 12)
136 //insert() 插入字符.其实,insert运用好,与其他的插入操作是一样的.
137 #include <string>
138 #include <iostream>
139 using namespace std;
140 void main()
141 {
142 string s = "hehe";
143 s.insert(0,"头部"); //在头部插入
144 s.insert(s.size(),"尾部"); //在尾部插入
145 s.insert(s.size()/2,"中间");//在中间插入
146 cout<<s<<endl;
147
148 }
149 13)
150 #include <string>
151 #include <iostream>
152 using namespace std;
153
154 void main()
155 {
156 string s = "abcdefg";
157 s.erase(0,1); //从索引0到索引1,即删除掉了'a'
158 cout<<s<<endl;
159 //其实,还可以使用replace方法来执行删除操作
160 s.replace(2,3,"");//即将指定范围内的字符替换成"",即变相删除了
161 cout<<s<<endl;
162
163 }
164
165 14)
166 //clear() 删除全部字符
167 #include <string>
168 #include <iostream>
169 using namespace std;
170
171 void main()
172 {
173 string s = "abcdefg";
174 cout<<s.length()<<endl;
175 s.clear();
176 cout<<s.length()<<endl;
177 //使用earse方法变相全删除
178 s = "dkjfd";
179 cout<<s.length()<<endl;
180 s.erase(0,s.length());
181 cout<<s.length()<<endl;
182
183
184 }
185 15)
186 //replace() 替换字符
187 #include <string>
188 #include <iostream>
189 using namespace std;
190
191 void main()
192 {
193 string s = "abcdefg";
194 s.replace(2,3,"!!!!!");//从索引2开始3个字节的字符全替换成"!!!!!"
195 cout<<s<<endl;
196
197 }
198 16)
199 //==,!=,<,<=,>,>=,compare() 比较字符串
200 #include <string>
201 #include <iostream>
202 using namespace std;
203
204 void main()
205 {
206 string s1 = "abcdefg";
207 string s2 = "abcdefg";
208 if (s1==s2)cout<<"s1 == s2"<<endl;
209 else cout<<"s1 != s2"<<endl;
210
211 if (s1!=s2)cout<<"s1 != s2"<<endl;
212 else cout<<"s1 == s2"<<endl;
213
214 if (s1>s2)cout<<"s1 > s2"<<endl;
215 else cout<<"s1 <= s2"<<endl;
216
217 if (s1<=s2)cout<<"s1 <= s2"<<endl;
218 else cout<<"s1 > s2"<<endl;
219
220 }
221 17)
222 //size(),length() 返回字符数量
223 #include <string>
224 #include <iostream>
225 using namespace std;
226
227 void main()
228 {
229 string s = "abcdefg";
230 cout<<s.size()<<endl;
231 cout<<s.length()<<endl;
232
233
234 }
235 18)
236 //max_size() 返回字符的可能最大个数
237 #include <string>
238 #include <iostream>
239 using namespace std;
240
241 void main()
242 {
243 string s = "abcdefg";
244 cout<<s.max_size()<<endl;
245
246
247 }
248 19)
249 //empty() 判断字符串是否为空
250 #include <string>
251 #include <iostream>
252 using namespace std;
253
254 void main()
255 {
256 string s ;
257 if (s.empty())
258 cout<<"s 为空."<<endl;
259 else
260 cout<<"s 不为空."<<endl;
261
262 s = s + "abcdefg";
263 if (s.empty())
264 cout<<"s 为空."<<endl;
265 else
266 cout<<"s 不为空."<<endl;
267
268
269 }
270 20)
271 // [ ], at() 存取单一字符
272 #include <string>
273 #include <iostream>
274 using namespace std;
275
276 void main()
277 {
278 string s = "abcdefg1111";
279
280 cout<<"use []:"<<endl;
281 for(int i=0; i<s.length(); i++)
282 {
283 cout<<s<<endl;
284 }
285 cout<<endl;
286
287 cout<<"use at():"<<endl;
288 for(int i=0; i<s.length(); i++)
289 {
290 cout<<s.at(i)<<endl;
291 }
292 cout<<endl;
293
294
295 }
296 21)
297 #include <string>
298 #include <iostream>
299 using namespace std;
300
301 void main()
302 {
303 string s = "abcdefg1111";
304
305 const char * chs1 = s.c_str();
306 const char * chs2 = s.data();
307
308 cout<<"use at():"<<endl;
309 int i;
310 for(i=0; i<s.length(); i++)
311 {
312 cout<<"c_str() : "<<chs1<<endl;
313 cout<<"data() : "<<chs2<<endl;
314 }
315 cout<<"c_str() : "<<chs1<<endl;
316 cout<<"data() : "<<chs2<<endl;
317 cout<<endl;
318
319
320 }
321 22)
322 // substr() 返回某个子字符串
323 #include <string>
324 #include <iostream>
325 using namespace std;
326
327 void main()
328 {
329 string s = "abcdefg1111";
330
331 string str = s.substr(5,3);//从索引5开始3个字节
332 cout<<str<<endl;
333
334
335 }
336 23)
337 // find 查找函数
338 s.find(args,pos) 在s 中从pos位置开始查找args 的第一次出现;
339 s.rfind(args,pos) 在s 中从pos位置开始查找args 的最后一次出现;
340 ;
341 s.find_first_not_of(args,pos) 在s 中从pos位置开始查找第一个不属于args 的字符;
342 s.find_last_not_of(args,pos) 在s 中从pos位置开始查找最后一个不属于args 的字符;
343 -------------------------------------------------------------------------------
344 ****** find 操作的参数: args ******
345 -------------------------------------------------------------------------------
346 c, pos 在s 中, 从下标pos 标记的位置开始, 查找字符c, pos 的默认值为0 ;
347 s2, pos 在s 中, 从下标pos 标记的位置开始, 查找string 对象s2, pos 的默认值为0
348
349 #include <string>
350 #include <iostream>
351 using namespace std;
352
353 void main()
354 {
355 string s = "abcdefg1111";
356 string pattern = "fg";
357 string::size_type pos;
358 pos = s.find(pattern,0); //从索引0开始,查找符合字符串"f"的头索引
359 cout<<pos<<endl;
360 string str = s.substr(pos,pattern.size());
361 cout<<str<<endl;
362
363 }
364 24)
365 // begin() end() 提供类似STL的迭代器支持
366 #include <string>
367 #include <iostream>
368 using namespace std;
369 void main()
370 {
371 string s = "abcdefg1111";
372 for(string::iterator iter = s.begin(); iter!=s.end(); iter++)
373 {
374 cout<<*iter<<endl;
375 }
376 cout<<endl;
377
378 }


        一个C++字符串存在三种大小:a)现有的字符数,函数是size()和length(),他们等效。 Empty()用来检查字符串是否为空。b)max_size() 这个大小是指当前C++字符串最多能包含的字符数,很可能和机器本身的限制或者字符串所在位置连续内存的大小有关系。我们一般情况下不用关心他,应该大小足够我们用的。但是不够用的话,会抛出length_error异常c)capacity()重新分配内存之前 string所能包含的最大字符数。这里另一个需要指出的是reserve()函数,这个函数为string重新分配内存。重新分配的大小由其参数决定,默认参数为0,这时候会对string进行非强制性缩减。

posted on 2012-03-28 09:10  专吃兔子的大黑猫  阅读(521)  评论(0)    收藏  举报