|
| 1 | +--- |
| 2 | +Title: '.append()' |
| 3 | +Description: 'Appends characters or strings to the end of an existing string.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Characters' |
| 9 | + - 'Concatenation' |
| 10 | + - 'Methods' |
| 11 | + - 'Strings' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +In C++, **`.append()`** is a method that concatenates new characters to the end of an existing string without requiring reassignment. It can be used to combine two different strings, append a portion of a string (also known as a substring), or add specific characters to the end of a string. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +str.append(str2); // Appends entire string |
| 23 | +str.append(str2, pos, len); // Appends substring from str2 |
| 24 | +str.append(n, char); // Appends character 'char' n times |
| 25 | +str.append(first, last); // Appends range using iterators |
| 26 | +``` |
| 27 | + |
| 28 | +**Parameters:** |
| 29 | + |
| 30 | +- `str2`: The string to append or extract a substring from. |
| 31 | +- `pos`: The starting index in `str2` (used when appending a substring). |
| 32 | +- `len`: The number of characters to append from `str2`, starting at `pos`. |
| 33 | +- `n`: The number of times to append the character `char`. |
| 34 | +- `char`: A character to be appended multiple times. |
| 35 | +- `first`, `last`: Iterators defining a range of characters to append. |
| 36 | + |
| 37 | +**Return value:** |
| 38 | + |
| 39 | +Returns a reference to the modified string (`*this`), allowing method chaining. |
| 40 | + |
| 41 | +## Example 1: Appending Two Strings Together |
| 42 | + |
| 43 | +This example takes two separate strings and concatenates them: |
| 44 | + |
| 45 | +```cpp |
| 46 | +#include <iostream> |
| 47 | +#include <string> |
| 48 | +using namespace std; |
| 49 | + |
| 50 | +int main () { |
| 51 | + string stringOne = "Hello "; |
| 52 | + string stringTwo = "World!"; |
| 53 | + |
| 54 | + //Appends stringTwo to the end of stringOne |
| 55 | + stringOne.append(stringTwo); |
| 56 | + |
| 57 | + cout << stringOne; |
| 58 | + return 0; |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +The output produced by this code is: |
| 63 | + |
| 64 | +```shell |
| 65 | +Hello World! |
| 66 | +``` |
| 67 | + |
| 68 | +## Example 2: Appending One Piece of a String to Another |
| 69 | + |
| 70 | +This example takes a portion of one string and adds it to the end of another string: |
| 71 | + |
| 72 | +```cpp |
| 73 | +#include <iostream> |
| 74 | +#include <string> |
| 75 | +using namespace std; |
| 76 | + |
| 77 | +int main() { |
| 78 | + string str1("Straw Hat "); |
| 79 | + string str2("Monkey D. Luffy"); |
| 80 | + |
| 81 | + // Appends 5 characters from index 10 of str2 to str1 |
| 82 | + str1.append(str2, 10, 5); |
| 83 | + |
| 84 | + cout << str1; |
| 85 | + return 0; |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +The output produced by this code is: |
| 90 | + |
| 91 | +```shell |
| 92 | +Straw Hat Luffy |
| 93 | +``` |
| 94 | + |
| 95 | +## Codebyte Example: Appending Multiple Characters to a String |
| 96 | + |
| 97 | +This codebyte example takes a specific character and adds it a specific amount of times to the end of a string: |
| 98 | + |
| 99 | +```codebyte/cpp |
| 100 | +#include <iostream> |
| 101 | +#include <string> |
| 102 | +using namespace std; |
| 103 | +
|
| 104 | +int main() { |
| 105 | + string str("Codecademy is awesome"); |
| 106 | +
|
| 107 | + // Appends 3 occurrences of '!' to the end of str |
| 108 | + str.append(3, '!'); |
| 109 | +
|
| 110 | + cout << str; |
| 111 | +
|
| 112 | + return 0; |
| 113 | +} |
| 114 | +``` |
0 commit comments