std::string::append() in C++ Last Updated : 20 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The string::append() in C++ is used append the characters or the string at the end of the string. It is the member function of std::string class .The string::append() function can be used to do the following append operations:Table of ContentAppend a Whole StringAppend a Part of the StringAppend a Character Multiple TimesAppend Characters from a Given RangeAppend a Whole StringThe string::append() method can be used to append the whole string at the end of the given string. We just need to pass the string to be appended as the parameter to string::append () function.Syntaxstr1.append(str2);Parameterstr1: String in which we have to append the string.str2: String which we have to append at the end.Return valueIt will return the original string after appending the string.Example C++ // C++ Program to show how to append the string // at the end of the given string #include <bits/stdc++.h> using namespace std; int main() { string str1("Hello World! "); string str2("GeeksforGeeks"); // Append the str2 in to str1 str1.append(str2); cout << str1 << endl; return 0; } OutputHello World! GeeksforGeeks Append a Part of the StringThe string::append() method can also be used to append the substring starting from the particular position till the given number of characters.Syntaxstr1.append(str2, pos, num);Parameterpos: Starting position of the substring from which we have to append.num: Number of characters which we have to append.Return valueIt will return the original string after appending the string.Example C++ // C++ Program for appending the substring at // the end of the string #include <bits/stdc++.h> using namespace std; int main() { string str1("Hello World! "); string str2("GeeksforGeeks "); // Appends 5 characters from 0th index of // str2 to str1 str1.append(str2, 0, 5); cout << str1; return 0; } OutputHello World! GeeksAppend a Character Multiple TimesThe string::append() method is used to append the multiple characters at the end of the string.Syntaxstr.append(num, c);Parameterstr: Name of the string in which we have to append the characters.c: Character which we have to append multiple times.num: Number of times up to which characters will append.Return valueIt will return the original string after appending the string.Example C++ // C++ Program for appending multiple copies // of the same character. #include <bits/stdc++.h> using namespace std; int main() { string str("Hello Geeks"); // Appends 5 occurrences of '!' // to str str.append(5, '!'); cout << str; return 0; } OutputHello Geeks!!!!!Append Characters from a Given RangeThe string::append() method can also be used to append the characters from the given range at the end of the string. Syntaxstr.append(first, last);Parameterfirst: Iterator pointing to the first position from which we have to append.last: Iterator pointing to the element just after the last element up to which we have to append.Return valueIt will return the original string after appending the string.Example C++ // C++ Program for appending the // string in the given range #include <bits/stdc++.h> using namespace std; int main() { string str1("Hello World! "); string str2("GeeksforGeeks"); // Appends all characters from // str2.begin()+5, str2.end() to str1 str1.append(str2.begin() + 5, str2.end()); cout << str1; return 0; } OutputHello World! forGeeks Comment More infoAdvertise with us Next Article std::string::append() in C++ S Sakshi Tiwari Improve Article Tags : Misc C++ STL CPP-Functions cpp-strings-library +1 More Practice Tags : CPPMiscSTL Similar Reads std::string::assign() in C++ The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents. Syntax 1: Assign the value of string str. string& string::assign (const string& str) str : is the string to be assigned. Returns : *this CPP // CPP code for assign 5 min read std::to_string in C++ In C++, the std::to_string function is used to convert numerical values into the string. It is defined inside <string> header and provides a simple and convenient way to convert numbers of any type to strings.In this article, we will learn how to use std::to_string() in C++.Syntaxstd::to_strin 1 min read std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.String vs Character ArrayStringChar 8 min read std::string::push_back() in C++ The std::string::push_back() method in C++ is used to append a single character at the end of string. It is the member function of std::string class defined inside <string> header file. In this article, we will learn about std::string::push_back() method in C++.Example:C++// C++ Program to ill 2 min read string at() in C++ The std::string::at() in C++ is a built-in function of std::string class that is used to extract the character from the given index of string. In this article, we will learn how to use string::at() in C++.Syntaxstr.at(idx)Parametersidx: Index at which we have to find the character.Return ValueReturn 1 min read std::string::data() in C++ The data() function writes the characters of the string into an array. It returns a pointer to the array, obtained from conversion of string to the array. Its Return type is not a valid C-string as no '\0' character gets appended at the end of array. Syntax: const char* data() const; char* is the po 2 min read std::string::size() in C++ The std::string::size() function in C++ is a built-in method of the std::string class that is used to determine the number of characters in the string. All characters up to the null character are considered while calculating the size of the string.In this article, we will learn about string::size() 2 min read std::string::insert() in C++ In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Geeks"; // Inserting another string at the friend // of s s.insert(s.size(), "forGeeks"); cou 4 min read std::string::resize() in C++ resize() lets you change the number of characters. Here we will describe two syntaxes supported by std::string::resize() in C++ Return Value : None Syntax 1: Resize the number of characters of *this to num. void string ::resize (size_type num) num: New string length, expressed in number of character 3 min read std::string::replace() in C++ The string::replace() function in C++ is used to replace a single or multiple characters from a given index. It is the member function of std::string class. In this article, we will learn how to use the string::replace() function in our C++ program.SyntaxThe string::replace() function provides 6 dif 6 min read Like