public member function
<string>

std::string::assign

string (1)
string& assign (const string& str);
substring (2)
string& assign (const string& str, size_t subpos, size_t sublen);
c-string (3)
string& assign (const char* s);
buffer (4)
string& assign (const char* s, size_t n);
fill (5)
string& assign (size_t n, char c);
range (6)
template <class InputIterator>   string& assign (InputIterator first, InputIterator last);
string (1)
string& assign (const string& str);
substring (2)
string& assign (const string& str, size_t subpos, size_t sublen);
c-string (3)
string& assign (const char* s);
buffer (4)
string& assign (const char* s, size_t n);
fill (5)
string& assign (size_t n, char c);
range (6)
template <class InputIterator>   string& assign (InputIterator first, InputIterator last);
initializer list(7)
string& assign (initializer_list<char> il);
move (8)
string& assign (string&& str) noexcept;
string (1)
string& assign (const string& str);
substring (2)
string& assign (const string& str, size_t subpos, size_t sublen = npos);
c-string (3)
string& assign (const char* s);
buffer (4)
string& assign (const char* s, size_t n);
fill (5)
string& assign (size_t n, char c);
range (6)
template <class InputIterator>   string& assign (InputIterator first, InputIterator last);
initializer list(7)
string& assign (initializer_list<char> il);
move (8)
string& assign (string&& str) noexcept;
Assign content to string
Assigns a new value to the string, replacing its current contents.

(1) string
Copies str.
(2) substring
Copies the portion of str that begins at the character position subpos and spans sublen characters (or until the end of str, if either str is too short or if sublen is string::npos).
(3) c-string
Copies the null-terminated character sequence (C-string) pointed by s.
(4) buffer
Copies the first n characters from the array of characters pointed by s.
(5) fill
Replaces the current value by n consecutive copies of character c.
(6) range
Copies the sequence of characters in the range [first,last), in the same order.
(7) initializer list
Copies each of the characters in il, in the same order.
(8) move
Acquires the contents of str.
str is left in an unspecified but valid state.

Parameters

str
Another string object, whose value is either copied or moved.
subpos
Position of the first character in str that is copied to the object as a substring.
If this is greater than str's length, it throws out_of_range.
Note: The first character in str is denoted by a value of 0 (not 1).
sublen
Length of the substring to be copied (if the string is shorter, as many characters as possible are copied).
A value of string::npos indicates all characters until the end of str.
s
Pointer to an array of characters (such as a c-string).
n
Number of characters to copy.
c
Character value, repeated n times.
first, last
Input iterators to the initial and final positions in a range. The range used is [first,last), which includes all the characters between first and last, including the character pointed by first but not the character pointed by last.
The function template argument InputIterator shall be an input iterator type that points to elements of a type convertible to char.
If InputIterator is an integral type, the arguments are casted to the proper types so that signature (5) is used instead.
il
An initializer_list object.
These objects are automatically constructed from initializer list declarators.

size_t is an unsigned integral type.

Return Value

*this

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// string::assign
#include <iostream>
#include <string>

int main ()
{
  std::string str;
  std::string base="The quick brown fox jumps over a lazy dog.";

  // used in the same order as described above:

  str.assign(base);
  std::cout << str << '\n';

  str.assign(base,10,9);
  std::cout << str << '\n';         // "brown fox"

  str.assign("pangrams are cool",7);
  std::cout << str << '\n';         // "pangram"

  str.assign("c-string");
  std::cout << str << '\n';         // "c-string"

  str.assign(10,'*');
  std::cout << str << '\n';         // "**********"

  str.assign<int>(10,0x2D);
  std::cout << str << '\n';         // "----------"

  str.assign(base.begin()+16,base.end()-12);
  std::cout << str << '\n';         // "fox jumps over"

  return 0;
}

Output:
The quick brown fox jumps over a lazy dog.
brown fox
pangram
c-string
**********
----------
fox jumps over


Complexity

Unspecified.
Unspecified, but generally linear in the new string length (and constant for the move version).

Iterator validity

Any iterators, pointers and references related to this object may be invalidated.

Data races

The object is modified.
The move assign form (8), modifies str.

Exception safety

For the move assign (8), the function does not throw exceptions (no-throw guarantee).
In all other cases, there are no effects in case an exception is thrown (strong guarantee).

If s does not point to an array long enough, or if the range specified by [first,last) is not valid, it causes undefined behavior.

If subpos is greater than str's length, an out_of_range exception is thrown.
If the resulting string length would exceed the max_size, a length_error exception is thrown.
A bad_alloc exception is thrown if the function needs to allocate storage and fails.

See also