字符串内容的变化包括修改和替换两种。本节将分别讲解字符串内容的修改和字符串内容的替换。
可以通过使用多个函数修改字符串的值。例如 assign(),operator=,erase(),交换(swap),插入(insert)等。另外,还可通过 append() 函数添加字符。
下面逐一介绍各成员函数的使用方法。
使用 assign() 函数可以直接给字符串赋值。该函数既可以将整个字符串赋值给新串,也可以将字符串的子串赋值给新串。其在 basic_string 中的原型为:
以上几种方法在例 1 中均有所体现。请读者参考下述代码。
【例 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'); //重复 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;
}
operator= 的功能就是赋值。
erase() 函数的原型为:
erase() 函数的使用方法为:
swap()函数的原型为:
swap()函数的使用方法为:
insert () 函数的原型为:
insert() 函数的使用方法为:
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() 函数的原型为:
append() 函数的使用方法为:
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;
}
程序运行结果:
如果在一个字符串中标识出具体位置,便可以通过下标操作修改指定位置字符的值,或者替换某个子串。完成此项操作需要使用 string 类的成员函数 replace()。
replace() 函数的原型如下:
该函数的使用方法参照下面的程序:
#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;
}
程序执行结果为:
本节讲述了诸多可进行字符串内容的修改和替换的函数及其使用方法,并给出了例题。由于每个函数可能有多个原型,希望读者根据自己的情况,掌握其中的一种或两种,以满足自己使用的需要。同时,希望读者能够对照例题的执行效果,认真阅读本章节中的源代码,彻底掌握本节内容。