cpp:std::string::append的用法

函数原型

    string& append (const string& str);
    	
    string& append (const string& str, size_t subpos, size_t sublen = npos);
    	
    string& append (const char* s);
    
    string& append (const char* s, size_t n);
    	
    string& append (size_t n, char c);
    
    template <class InputIterator>   string& append (InputIterator first, InputIterator last);
    
    string& append (initializer_list<char> il);

用法解释

	string str1 = "hello";
	/*
	        string& append (const string& str);
	        参数1:需要追加的string
	        功能:将str追加到string的后面
	    */
	string tmp = ",cpp";
	str1 = "hello";
	string str2 = str1.append(tmp); // 结果为hello,cpp
	
	/*
	        string& append (const string& str, size_t subpos, size_t sublen = npos);
	        参数1:添加的string
	        参数2:str的subpos位置
	        参数3:截取str的数量
	        功能:从str的subpos处,截取sublen放到string的后面
	    */
	str1 = "hello";
	string str3 = str1.append(tmp, 1, 3); // 结果为hellocpp
	
	/*
	        string& append (const char* s);
	        参数1:s为c风格的字符串
	        功能:将s添加的原string后面,并且返回最终的string
	    */
	str1 = "hello";
	string str4 = str1.append(",cpp"); // 结果为hello,cpp
	
	/*
	        string& append (const char* s, size_t n);
	        参数1:追加的字符串
	        参数2:追加的数量
	        功能:追加字符串s的前n个字符
	    */
	str1 = "hello";
	string str5 = str1.append(",cpphh", 4); // 结果为hello,cpp
	
	/*
	        string& append (size_t n, char c);
	        参数1:数量n个
	        参数2:追加的字符
	        功能:在字符串后面追加n个c
	    */
	str1 = "hello";
	string str6 = str1.append(3, 'c');  //结果为helloccc

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ht巷子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值