c++中 append()函数用法

c++中 append()函数用法append()函数常用的函数原型是:basic_string&append(constbasic_string&str);basic_string&append(constchar*str);basic_string&append(constbasic_string&str,size_typeindex,size_typelen);basic_string&append(constchar

大家好,又见面了,我是你们的朋友全栈君。

string::append官方介绍网址
append()函数:是向string 的后面追加字符或字符串。

常用的函数原型、简例:

1.在字符串的末尾添加字符串str

  string& append (const string& str);	
  string& append (const char* s);

1)在string的末尾添加string。如下:

string s1="hello";
string s2= "the";
string s3="world";
s1.append(s2);  //把字符串s连接到当前字符串的结尾
s1+=s3;
s1="hello the";
s1="hello the world";

2)在string的末尾添加C-string。把c类型字符串s连接到当前字符串结尾,如下:

string s = "hello";
const char*c="world";
s.append(c);   
s="hello world";

2.在字符串的末尾添加字符串str的子串,子串以subpos索引开始,长度为sublen

string& append (const string& str, size_t subpos, size_t sublen);

1)在string的后面添加string的一部分,即一个子串,子串以4索引开始,长度为5。如下:

string s1 = "hello";
string s2 = "the world";
s1.append(s2,4,5);  //把字符串从s2中从4开始的5个字符连接到当前字符串的结尾
s1 = "hello world";

2)若是添加的子串中只有索引开始的位置,没有长度,则表示字符串从第n个字符到末尾的字符连接到当前字符串末尾,如下:

string s1 = "hello";
string s2 = "the world";
s1.append(s2, 3);
运行结果为:s1="hello world"

3.在字符串的末尾添加字符串s中的前n个字符

string& append (const char* s, size_t n)

在string的后面添加C-string的一部分。把c类型字符串c的前n个字符连接到当前字符串结尾,如下:

string s = "hello";
const char*c = "the world";
s.append(c,3);
运行结果为:s="hellothe";

4.在字符串的末尾添加n个字符c;

string& append (size_t n, char c);

在string后面添加多个相同字符,如下:

  string s1 = "hello";
  s1.append(3, '!'); //在当前字符串结尾添加3个字符!
  运行结果为 s1 = "hello!!!";

5)在字符串的末尾添加以迭代器first和last表示的字符序列

string& append (InputIterator first, InputIterator last););

把s2的迭代器begin()+4和end()之间的部分连接到当前字符串的结尾

string s1 = "hello"
string s2 = "the world";
s1.append(s2.begin()+4,s2.end());//把s2的迭代器begin()+4和end()之间的部分连接到当前字符串的结尾
运行结果为:s1 = "hello world";

例1.string str4(“Wow”);与string str4 = “wow”;等同。

#include<iostream> 
#include <string>
using namespace std;

int main()
{ 
   
	string str1 = "Hey";
	string str2 = ",look the world.";
	string str3 = "Hello";
	//string str4("Wow");
	string str4 = "wow";
	//................................................

	str1.append(str2);
	//str3.append(str2, 5);
	str3.append(str2, 5, 11);
	str4.append(5, '.');
    //................................................

	cout << str1 << endl;
	cout << str3 << endl;
	cout << str4 << endl;
	system("pause");

	return 0;

}

运行结果为

Hey,look the world.
Hello the world.
wow.....
请按任意键继续. . .

例2.

// appending to string
#include <iostream>
#include <string>

int main()
{ 
   
	std::string str;
	std::string str2 = "Writing ";
	std::string str3 = "print 10 and then 5 more";

	// used in the same order as described above:
	str.append(str2);                       // "Writing "
	str.append(str3, 6, 3);                   // "10 "
	str.append("dots are cool", 5);          // "dots "
	str.append("here: ");                   // "here: "
	str.append(10u, '.');                    // ".........."
	str.append(str3.begin() + 8, str3.end());  // " and then 5 more"
	//str.append<int>(5, 0x2E); // "....."

	std::cout << str << '\n';
	return 0;
}

运行结果为

Writing 10 dots here: .......... and then 5 more
请按任意键继续. . .



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150525.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Web前端开发实战4:导航菜单(一)「建议收藏」

    Web前端开发实战4:导航菜单(一)「建议收藏」在前面的博文中我们提到横向一级菜单,这里我们来看看导航菜单。导航菜单种类很多,但是制作原理都是大同小异的,这里看的比二级下拉式菜单还简单。来看一些网站上的导航菜单:    垂直导航菜单:    水平导航菜单:    一垂直菜单   制作原理:(1)用无序列表构建菜单;(2)标签的设置:ullia{display:block;}。定义的

    2022年7月26日
    15
  • C# DialogResult.OK == Form.ShowDialog()

    C# DialogResult.OK == Form.ShowDialog()简要的分析:是将窗体显示为模式窗体并判断窗体的返回值来确定下一步的操作。DialogResult是Form类的一个属性,ShowDialog()是Form类的一个方法也就是函数。一般对话框上会有:确定,取消两个按钮。点击确定,会设置DialogResult=DialogResult.OK;点击取消,会设置DialogResult=DialogResult.Cancel;

    2022年6月22日
    83
  • freemarker错误九

    freemarker错误九

    2022年1月3日
    49
  • django配置文件详解_pycharm配置python

    django配置文件详解_pycharm配置python前言Django的配置文件settings.py用于配置整个网站的环境和功能,核心配置必须有项目路径、密钥配置、域名访问权限、App列表、中间件、资源文件、模板配置、数据库的连接方式基本配置信息

    2022年7月28日
    10
  • APK签名机制之——V2签名机制详解「建议收藏」

    APK签名机制之——V2签名机制详解「建议收藏」通过前一篇《Apk签名机制之——JAR签名机制详解》的分析我们知道,JAR签名需要对apk内所有文件进行hash校验,当资源较多时签名验证速度较慢。为了加快验证速度并加强完整性保证,Andorid在7.0引入一种全文件签名方案V2。下面来看V2方案的具体设计原理。

    2022年5月10日
    46
  • enableEventValidation是干什么的?

    enableEventValidation是干什么的?回发或回调参数无效。在配置中使用<pagesenableEventValidation=”true”/>或在页面中使用<%@PageEnableEventValidation=”true”%>启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用ClientScriptMana…

    2022年7月26日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号