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
filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code for assign (const string& str)
   
#include <iostream>
#include <string>
using namespace std;
   
// Function to demonstrate assign
void assignDemo(string str1, string str2)
{
    // Assigns str2 to str1
    str1.assign(str2);
    cout << "After assign() : ";
    cout << str1;
  
}
          
// Driver code
int main()
{
    string str1("Hello World!");
    string str2("GeeksforGeeks");
  
    cout << "Original String : " << str1 << endl;
    assignDemo(str1, str2);
   
    return 0;
}

chevron_right


Output:

Original String : Hello World!
After assign() : GeeksforGeeks

Syntax 2: Assigns at most str_num characters of str starting with index str_idx. It throws out_of _range if str_idx > str. size().

string& string::assign (const string& str, size_type str_idx, size_type str_num)

str : is the string to be assigned.
str_idx : is the index number in str.
str_num : is the number of characters picked 
from str_idx to assign
Return : *this
filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to illustrate
// assign(const string& str, size_type str_idx, size_type str_num)
   
#include <iostream>
#include <string>
using namespace std;
   
// Function to demonstrate assign
void assignDemo(string str1, string str2)
{
    // Assigns 13 charaacters from
    // 5th index of str2 to str1
    str1.assign(str2, 5, 13);
    cout << "After assign() : ";
    cout << str1;
  
}
          
// Driver code
int main()
{
    string str1("Hello World!");
    string str2("GeeksforGeeks");
  
    cout << "Original String : " << str1 << endl;
    assignDemo(str1, str2);
   
    return 0;
}

chevron_right


Output:

Original String : Hello World!
After assign() : forGeeks

Syntax 3: Assign the characters of the C-string cstr. It throws length_error if the resulting size exceeds the maximum number of characters.

string & string::assign (const char* cstr)

Assigns all characters of cstr up to but not including '\0'.
Returns : *this.
Note : that cstr may not be a null pointer (NULL).
filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code for assign (const char* cstr)
   
#include <iostream>
#include <string>
using namespace std;
   
// Function to demonstrate assign
void assignDemo(string str)
{
    // Assigns GeeksforGeeks to str
    str.assign("GeeksforGeeks");
    cout << "After assign() : ";
    cout << str;
  
}
          
// Driver code
int main()
{
    string str("Hello World!");
  
    cout << "Original String : " << str << endl;
    assignDemo(str);
   
    return 0;
}

chevron_right


Output:

Original String : Hello World!
After assign() : GeeksforGeeks

Syntax 4: Assigns chars_len characters of the character array chars. It throws length_error if the resulting size exceeds the maximum number of characters.

string& string::assign (const char* chars, size_type chars_len)

*chars : is the pointer to the array to be assigned.
chars_len : is the number of characters to be assigned from 
character array.
Note : that chars must have at least chars_len characters.
Returns : *this.
filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to illustrate 
// string& string::assign (const char* chars, size_type chars_len)
   
#include <iostream>
#include <string>
using namespace std;
   
// Function to demonstrate assign
void assignDemo(string str)
{
    // Assigns first 5 characters of
    // GeeksforGeeks to str
    str.assign("GeeksforGeeks", 5);
    cout << "After assign() : ";
    cout << str;
  
}
          
// Driver code
int main()
{
    string str("Hello World!");
  
    cout << "Original String : " << str << endl;
    assignDemo(str);
   
    return 0;
}

chevron_right


Output:

Original String : Hello World!
After assign() : Geeks

Synatx 5: Assigns num occurrences of character c. It throws length_error if num is equal to string::npos

string & string::assign (size_type num, char c)

num :  is the number of occurrences to be assigned.
c :  is the character which is to be assigned repeatedly. 
Throws length_error if the resulting size exceeds the maximum number(max_size) of characters.
Returns : *this.
filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code for string& string::assign (size_type num, char c)
   
#include <iostream>
#include <string>
using namespace std;
   
// Function to demonstrate assign
void assignDemo(string str)
{
    // Assigns 10 occurrences of 'x'
    // to str
    str.assign(10, 'x');
    cout << "After assign() : ";
    cout << str;
  
}
          
// Driver code
int main()
{
    string str("#########");
  
    cout << "Original String : " << str << endl;
    assignDemo(str);
   
    return 0;
}

chevron_right


Output:

Original String : #########
After assign() : xxxxxxxxxx

Syntax 6: Assigns all characters of the range [beg, end). It throws length_error if range outruns the actual content of string.

template <class InputIterator>
   string& assign (InputIterator first, InputIterator last)
first, last : Input iterators to the initial and final positions 
in a sequence.

Returns : *this.
filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code for string& string::assign (size_type num, char c)
   
#include <iostream>
#include <string>
using namespace std;
   
// Function to demonstrate assign
void assignDemo(string str)
{
    string str1;
    // Assigns all characters between
    // str.begin()+6 and str.end()-0 to str1
    str1.assign(str.begin()+6, str.end()-0);
    cout << "After assign() : ";
    cout << str1;
  
}
          
// Driver code
int main()
{
    string str("Hello World!");
  
    cout << "Original String : " << str << endl;
    assignDemo(str);
   
    return 0;
}

chevron_right


Output:

Original String : Hello World!
After assign() : World!

This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.