字符串赋值操作

&1 相关函数assign的五种初始化方法

  • 用常量字符串赋值,下面代码中的str1;
  • 直接用另一个字符串赋值,见str2;
  • 用一个字符串的前一段子串赋值,str3;
  • 用另一个字符串的一个子串赋值,str4;
  • 用几个相同的字符,赋值,见str5.

&2 测试源代码 

string str1("sophia is a good girl!");
string str2;
string str3;
string str4;
string str5;
str2.assign(str1);
str3.assign("sophia is a good girl!", 8);
str4.assign(str1, 2, 10);
str5.assign(10, 'c');//用几个相同的字符,赋值.
cout << "str1= " << str1 << endl;
cout << "str2= " << str2 << endl;
cout << "str3= " << str3 << endl;
cout << "str4= " << str4 << endl;
cout << "str5= " << str5 << endl;

原文地址:https://www.cnblogs.com/sophia-hxw/p/5670883.html