|
| 1 | +--- |
| 2 | +Title: '.insert()' |
| 3 | +Description: 'Inserts characters or strings at a specified position within 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++, **`.insert()`** is a method used to insert characters, substrings, or ranges into a string at a specified position. Unlike [`.append()`](https://www.codecademy.com/resources/docs/cpp/strings/append), which only adds content to the end, `.insert()` allows placing new data at any point within the string. It is commonly used in formatting output, inserting delimiters, modifying user input, or dynamically building strings during parsing and text processing tasks. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +string.insert(pos, str); |
| 23 | +string.insert(pos, str, subpos, sublen); |
| 24 | +string.insert(pos, n, char); |
| 25 | +string.insert(iterator, char); |
| 26 | +string.insert(iterator, n, char); |
| 27 | +
|
| 28 | +template<class InputIterator> |
| 29 | +string.insert(iterator, InputIterator first, InputIterator last); |
| 30 | +``` |
| 31 | + |
| 32 | +**Parameters:** |
| 33 | + |
| 34 | +- `pos`: The index position in the string where the insertion begins. |
| 35 | +- `str`: The string or character array to insert. |
| 36 | +- `subpos`: The starting position in `str` for partial insertion. |
| 37 | +- `sublen`: The number of characters to insert from `str` starting at `subpos`. |
| 38 | +- `n`: The number of times the character should be inserted. |
| 39 | +- `char`: The character to be inserted. |
| 40 | +- `iterator`: An iterator pointing to the insertion position within the string. |
| 41 | +- `first`, `last`: A range of iterators defining the characters to insert. |
| 42 | + |
| 43 | +**Return value:** |
| 44 | + |
| 45 | +Returns a reference to the modified string (`*this`), enabling method chaining. |
| 46 | + |
| 47 | +## Example 1: Inserting a Substring |
| 48 | + |
| 49 | +This example demonstrates inserting part of one string into another at a specific index: |
| 50 | + |
| 51 | +```cpp |
| 52 | +#include <iostream> |
| 53 | +#include <string> |
| 54 | +using namespace std; |
| 55 | + |
| 56 | +int main () { |
| 57 | + string str1 = "Hello World!"; |
| 58 | + string str2 = "Amazing "; |
| 59 | + |
| 60 | + // Inserts "Amazing " at index 6 in str1 |
| 61 | + str1.insert(6, str2); |
| 62 | + |
| 63 | + cout << str1; |
| 64 | + return 0; |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +The output produced by this code is: |
| 69 | + |
| 70 | +```shell |
| 71 | +Hello Amazing World! |
| 72 | +``` |
| 73 | + |
| 74 | +## Example 2: Inserting a Portion of a String |
| 75 | + |
| 76 | +This example takes a portion of one string and inserts it into another string at a specific position: |
| 77 | + |
| 78 | +```cpp |
| 79 | +#include <iostream> |
| 80 | +#include <string> |
| 81 | +using namespace std; |
| 82 | + |
| 83 | +int main() { |
| 84 | + string str1 = "C++ is great!"; |
| 85 | + string str2 = "really awesome and "; |
| 86 | + |
| 87 | + // Inserts 8 characters starting from index 7 of str2 into str1 at position 7 |
| 88 | + str1.insert(7, str2, 7, 8); |
| 89 | + |
| 90 | + cout << str1; |
| 91 | + return 0; |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +The output produced by this code is: |
| 96 | + |
| 97 | +```shell |
| 98 | +C++ is awesome great! |
| 99 | +``` |
| 100 | + |
| 101 | +## Codebyte Example: Inserting Multiple Characters |
| 102 | + |
| 103 | +This codebyte demonstrates inserting multiple characters at a specific position in a string: |
| 104 | + |
| 105 | +```codebyte/cpp |
| 106 | +#include <iostream> |
| 107 | +#include <string> |
| 108 | +using namespace std; |
| 109 | +
|
| 110 | +int main() { |
| 111 | + string str = "C++ is fun"; |
| 112 | +
|
| 113 | + // Insert three '*' characters at index 5 |
| 114 | + str.insert(5, 3, '*'); |
| 115 | +
|
| 116 | + cout << str; |
| 117 | +
|
| 118 | + return 0; |
| 119 | +} |
| 120 | +``` |
0 commit comments