std::string::assign() in C++
Last Updated :
28 Oct, 2020
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
CPP
#include <iostream>
#include <string>
using namespace std;
void assignDemo(string str1, string str2)
{
str1.assign(str2);
cout << "After assign() : " ;
cout << str1;
}
int main()
{
string str1( "Hello World!" );
string str2( "GeeksforGeeks" );
cout << "Original String : " << str1 << endl;
assignDemo(str1, str2);
return 0;
}
|
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
CPP
#include <iostream>
#include <string>
using namespace std;
void assignDemo(string str1, string str2)
{
str1.assign(str2, 5, 8);
cout << "After assign() : " ;
cout << str1;
}
int main()
{
string str1( "Hello World!" );
string str2( "GeeksforGeeks" );
cout << "Original String : " << str1 << endl;
assignDemo(str1, str2);
return 0;
}
|
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).
CPP
#include <iostream>
#include <string>
using namespace std;
void assignDemo(string str)
{
str.assign( "GeeksforGeeks" );
cout << "After assign() : " ;
cout << str;
}
int main()
{
string str( "Hello World!" );
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
|
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.
CPP
#include <iostream>
#include <string>
using namespace std;
void assignDemo(string str)
{
str.assign( "GeeksforGeeks" , 5);
cout << "After assign() : " ;
cout << str;
}
int main()
{
string str( "Hello World!" );
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
|
Output:
Original String : Hello World!
After assign() : Geeks
Syntax 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.
CPP
#include <iostream>
#include <string>
using namespace std;
void assignDemo(string str)
{
str.assign(10, 'x' );
cout << "After assign() : " ;
cout << str;
}
int main()
{
string str( "#########" );
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
|
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.
CPP
#include <iostream>
#include <string>
using namespace std;
void assignDemo(string str)
{
string str1;
str1.assign(str.begin()+6, str.end()-0);
cout << "After assign() : " ;
cout << str1;
}
int main()
{
string str( "Hello World!" );
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
|
Output:
Original String : Hello World!
After assign() : World!
If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected].
Similar Reads
std::basic_string::at in C++
Returns a reference to the character at the specified location pos. The function automatically checks whether pos is the valid position of a character in the string (i.e., whether pos is less than the string length), throwing an out_of_range exception if it is not. Syntax: reference at (size_type po
1 min read
std::string::append() in C++
The string::append() in C++ is used append the characters or the string at the end of the string. It is the member function of std::string class . The string::append() function can be used to do the following append operations: Table of Content Append a Whole StringAppend a Part of the StringAppend
3 min read
std::string class in C++
C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character. String vs Character ArrayString Ch
8 min read
std::vector::assign() in C++ STL
In C++, the vector assign() is a built-in method used to assign the new values to the given vector by replacing old ones. It also modifies the size of the vector according to the given number of elements. Letâs take a look at an example that shows the how to use this function. [GFGTABS] C++ #include
4 min read
std::to_string in C++
In C++, the std::to_string function is used to convert numerical values into the string. It is defined inside <string> header and provides a simple and convenient way to convert numbers of any type to strings. In this article, we will learn how to use std::to_string() in C++. Syntaxstd::to_str
1 min read
std::string::clear in C++
The string content is set to an empty string, erasing any previous content and thus leaving its size at 0 characters. Parameters: none Return Value: none void string ::clear () - Removes all characters (makes string empty) - Doesn't throw any error - Receives no parameters and returns nothing // CPP
1 min read
std::string::insert() in C++
In C++, the string::insert() function is used insert the characters or a string at the given position of the string. It is the member function of std::string class. The string::insert method can be used in the following ways to: Table of Content Insert a Single CharacterInsert a Single Character Mul
4 min read
std::basic_string::operator[] in C++
Returns a reference to the character at specified location pos. No bounds checking is performed. If pos > size(), the behavior is undefined. Syntax : reference operator[] (size_type pos); const_reference operator[] (size_type pos) const; Parameters : pos - position of the character to return Retu
1 min read
std::to_wstring in c++
This function is used to convert the numerical value to the wide string i.e. it parses a numerical value of datatypes (int, long long, float, double ) to a wide string. It returns a wide string of data type wstring representing the numerical value passed in the function. In this function data type i
3 min read
std::basic_string::max_size in C++
std::basic_string::max_size is used to compute the maximum number of elements the string is able to hold due to system or library implementation limitations. size_type max_size() const; Parameters : None Return value : Maximum number of characters Exceptions : None // CPP program to compute the limi
1 min read