C++ String Assign Function - C++ Programming Tutorial
C++ Course / Strings / C++ String Assign Function

C++ String Assign Function

BLUF: Mastering C++ String Assign Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ String Assign Function

C++ is renowned for its efficiency. Learn how C++ String Assign Function enables low-level control and high-performance computing in the tutorial below.

In C++, the assign method is frequently used to replace the current content of a string with new content. This method provides various overloaded variations that enable us to duplicate data from another string, C-string, array of characters, substring, or recurring characters.

In simpler terms, the assign function in C++ modifies the current content of a string with fresh information. It provides multiple crucial techniques for assigning values, like specifying characters, C-strings, substrings, and repeating characters within the string.

Syntax:

The assign function for strings is structured as follows:

Example

string.assign(const string& str);

string.assign(const string& str, size_t pos, size_t len);

string.assign(const char* st);

string.assign(const char* st, size_t n);

string.assign(size_t n, char ch);

string.assign(InputIterator first, InputIterator last);

In this syntax,

  • str: It represents a string object whose value is to be assigned.
  • pos: It defines the position of the character, which is to be copied as a substring.
  • len: It determines the number of characters of the string to be copied into another string object.
  • n: It is used to represent the number of characters to copy.
  • ch: It is used to represent the character value to be copied n times.
  • Simple C++ String Assign Function Example

Let's explore an instance to showcase the string assign method in C++.

Example

Example

#include<iostream>  

using namespace std;    // using standard namespace

int main()    // main function

{  

string str = "Logic </> practice";  

string str1;  

str1.assign(str);  

cout<<"Assigned string is : " <<str1;  

return 0;  

}

Output:

Output

Assigned string is : Logic </> practice

Explanation:

In this particular scenario, we demonstrate the process by which the assign method duplicates the content of one string to another. Initially, an empty string denoted as str1 is created, followed by the utilization of str1.assign(str) to transfer the content of str into str1. Subsequently, str1 now holds identical text as str. To conclude, the assigned string is exhibited on the display.

C++ string assign function Example using Position and Length Parameters

Here, we are presenting a basic illustration to showcase the implementation of the assign method for strings in C++, where both the starting position and length are specified as parameters.

Example

Example

#include<iostream>  

using namespace std;    // using standard namespace

int main()   // main function

{  

string str = "C++ is an object-oriented programming language";  

string str1;  

str1.assign(str,7,20) ;  

cout<<str1;  

return 0;  

}

Output:

Output

an object-oriented p

Explanation:

In this instance, we demonstrate the functionality of the assign method in extracting and duplicating a particular segment of a string. Initially, we utilize the str1.assign(str, 7, 20) method, which initiates the copying process from the 7th index of the 'str' string and selects 20 characters from that point onwards. Consequently, solely the specified substring is retained in the 'str1' string variable. Subsequently, the program exclusively displays the extracted segment of the original string.

C++ String Assign Function Example to Assign First N Characters from a C-String

Let's consider a basic example to showcase the string assign method for assigning the initial N characters from a C-style string in C++.

Example

Example

#include<iostream>  

using namespace std;    // using standard namespace

int main()  // main function

{  

string st;  

st.assign("C++ Programming Language Tutorial",10);  

cout<<"The assigned string is : " <<st;  

return 0;   

}

Output:

Output

The assigned string is : C++ Progra

Explanation:

In this instance, we demonstrate the functionality of the assign method in copying a specified number of characters from a C-style string to a string object. The command st.assign("C++ Programming Language Tutorial", 10) specifically copies the initial 10 characters from the provided string literal. Consequently, the string st contains a shortened version of the original content, displaying the assigned substring.

C++ string assign function Example to Assign Multiple Copies of a Character

Let's consider an example to demonstrate how to assign multiple occurrences of a character within a string using the assign function in C++.

Example

Example

#include<iostream>

#include<string>

using namespace std;    // using standard namespace

int main()    // main function

{  

string st;  

st.assign(10, 'P');  

cout<<st<<endl;  

return 0;   

}

Output:

Output

PPPPPPPPPP

Explanation:

In this provided illustration, we showcase the assign method within C++ strings, which serves to substitute the existing string content with a defined value. Here, we have employed the st.assign(10, 'P') method, specifically intended to allocate 10 instances of the same character. Ultimately, the method outputs the freshly assigned string to the display.

Features of the String Assign function in C++

The string assign function has several features in the C++ programming language. Some of them are as follows:

  • The string assign function in C++ is commonly utilized to clear the existing content from the string and assign the new data to the string.
  • We can use this function to manage the memory allocation internally for the newly assigned data in the string.
  • We can use this function to perform operations with both std::string and std::basic_string.
  • It can be utilized to assign new characters from the range defined by iterators.
  • This function offers an effective method to create or modify the string without manually clearing or concatenating the string in C++.
  • It also helps to return a reference to the updating, which allows operation chaining in C++.
  • Conclusion

In summary, the assign function in C++ offers a versatile and efficient approach to substituting or altering the current string content with fresh content. It offers versatility for different types of assignments like substrings, C-style strings, iterators, and repeated characters, simplifying string manipulation. This function aids in maintaining dependable and convenient string operations within C++ programs by managing memory internally and ensuring robust exception handling.

C++ String Assign Function FAQs

1) What is the string assign function in C++?

The assign method in C++ is frequently used to erase the current content within the string and replace it with new data. It offers multiple overloaded variations that enable us to duplicate information from a different string, C-string, array of characters, substring, or recurring characters.

No, the assign function in C++ does not delete the previous data of the string.

Yes, we have the option to utilize the assign method to clear the current content within the string and then insert new data or make modifications to it.

3) Does the assign method automatically manage memory allocation in C++?

Yes, the assign method allows us to dynamically allocate memory for the string data being assigned, as needed.

4) Is it possible for the assign method in C++ to only substitute a specific segment of the string?

Yes, in C++, we have the ability to substitute a portion of a string by indicating both the initial position and the length of the substring.

5) What value does the assign method return in C++?

In C++, the assign method in strings provides a reference to the updated string, allowing for method chaining.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience