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
// 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;
}
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
// 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 8 characters from
// 5th index of str2 to str1
str1.assign(str2, 5, 8);
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;
}
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
// 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;
}
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
// 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;
}
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
// 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;
}
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
// 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;
}
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
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Unified Modeling Language (UML) Diagrams Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan
14 min read
Waterfall Model - Software Engineering The Waterfall Model is a Traditional Software Development Methodology. It was first introduced by Winston W. Royce in 1970. It is a linear and sequential approach to software development that consists of several phases. This classical waterfall model is simple and idealistic. It is important because
13 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read