c++中string::function集合

string append()

  1.直接添加另一个完整的字符串:

   str1.append(str2);

  2.添加另一个字符串的某一段字串:

     str1.append(str2, 11,  7);  //添加str2中第11字符之后的7个字符

  3.添加n个相同的字符;

     str1.append(n, '-');  //在str1中添加n个“-”

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     string str1 = "I like c++";
 7     string str2 = "the so nice weather";
 8     string str3 = "hello";
 9     string str4("hello world");
10     
11     str1.append(str2);
12     str3.append(str2, 11, 7);   //重点
13     str4.append(5, '-');
14     
15     std::cout<<"str1 = "<<str1<<std::endl;
16     std::cout<<"str2 = "<<str2<<std::endl;
17     std::cout<<"str3 = "<<str3<<std::endl;
18     std::cout<<"str4 = "<<str4<<std::endl;
19     return 0;
20 }
-----输出:
  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');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值