.assign()
In C++, the .assign()
method replaces the contents of a string with new characters, allowing precise control over what the string holds. It is commonly used in scenarios such as loops, input parsing, or reusing string variables to optimize memory and performance.
Syntax
string.assign(str); // Assign entire string
string.assign(str, subpos, sublen); // Assign substring from another string
string.assign("text"); // Assign from a C-style null-terminated string
string.assign("text", n); // Assign first n characters from a C-string
string.assign(n, ch); // Assign n copies of character ch
string.assign(first, last); // Assign characters from iterator range
Parameters:
str
: Anotherstd::string
whose contents will be assigned to the string.subpos
: The starting index instr
from which to begin copying.sublen
: The number of characters to copy fromstr
starting atsubpos
.s
: A C-style null-terminated string (const char*
) to assign from.n
:- With C-string: Number of characters to copy from
s
. - With character: Number of times to repeat the character
ch
.
- With C-string: Number of characters to copy from
ch
: A character to be repeatedn
times.first
: Input iterator pointing to the beginning of the range to assign.last
: Input iterator pointing one past the end of the range.
Return value:
string&
: Reference to the modified string (*this
) for method chaining
Example 1: Assigning a String to Another
In this example, .assign()
is called on the greeting
string to copy the value from the name
string:
#include <iostream>#include <string>int main() {std::string greeting = "Hello";std::string name = "World";// Replaces the contents of greeting with namegreeting.assign(name);std::cout << greeting;return 0;}
The output of this code is:
World
Example 2: Assigning a Substring
In this example, .assign()
is called on the result
string to copy a portion of the sentence
string:
#include <iostream>#include <string>int main() {std::string sentence = "Codecademy Docs";std::string result;// Assigns the first 9 characters of sentence to resultresult.assign(sentence, 0, 9);std::cout << result;return 0;}
The output of this code is:
Codecadem
Example 3: Assigning Using Iterators
In this example, .assign()
is used to copy a set of characters from the source
string into the result
string using iterators:
#include <iostream>#include <iostream>#include <string>int main() {std::string source = "Version Control";std::string result;// Assigns characters from index 0 to 7 (exclusive)result.assign(source.begin(), source.begin() + 7);std::cout << result;return 0;}
The output of this code is:
Version
Codebyte Example: Assigning Repeated Characters
In this codebyte example, .assign()
is used to assign the line
string with five asterisk characters:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours