string append()
1.直接添加另一个完整的字符串:
str1.append(str2);
2.添加另一个字符串的某一段字串:
str1.append(str2, 11, 7); //添加str2中第11字符之后的7个字符
3.添加n个相同的字符;
str1.append(n, '-'); //在str1中添加n个“-”
#include <iostream>
using namespace std; int main()
{
string str1 = "I like c++";
string str2 = "the so nice weather";
string str3 = "hello";
string str4("hello world"); str1.append(str2);
str3.append(str2, , ); //重点
str4.append(, '-'); std::cout<<"str1 = "<<str1<<std::endl;
std::cout<<"str2 = "<<str2<<std::endl;
std::cout<<"str3 = "<<str3<<std::endl;
std::cout<<"str4 = "<<str4<<std::endl;
return ;
}
-----输出:
str1 = I like c++the so nice weather
str2 = the so nice weather
str3 = hello weathe
str4 = hello world-----
string assign()
函数assign()常给string变量赋值;
1.直接用另一个字符串赋值
str1.assign(str2); //用str2给str1赋值;
2. 用另一个字符串的子串赋值
str3.assign(str2, 4, 5);
3.用一个字符串的前一段子串赋值
str4.assign("World", 5);
4.用几个相同的字符赋值
str5.assign(10, 'c');