TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ String assign() function

C++ String assign() function tutorial for beginners and professionals with examples on constructor, if-else, switch, break, continue, comments, arrays, object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

C++ String Assign()

This functions assigns a new value to the string,replacing all its current contents.

Syntax

Consider two strings str1 and str2, Syntax would be :

Str1.assign(str2);

Parameters

str : str is a string object, whose value to be assigned.

subpos : It defines the position of the character which is to be copied as a substring.

sublen : It determines the number of characters of string to be copied in another string object.

n : Number of characters to copy.

ch : Character value to be copied n times

Return value

*this

Example 1

Let's see simple example.

#include<iostream>
using namespace std;
int main()
{
string str = "TheDeveloperBlog";
string str1;
str1.assign(str);
cout<<"Assigned string is : " <<str1;
return 0;
} 

Output:

Assigned string is :TheDeveloperBlog

Example 2

Let's see simple example when position and length are mentioned in the parameters.

#include<iostream>
using namespace std;
int main()
{
string str = "C is a programming language";
string str1;
str1.assign(str,7,20) ;
cout<<str1;
return 0;
} 

Output:

programming language

Example 3

Let's see simple example when n is given.

#include<iostream>
using namespace std;
int main()
{
string s;
s.assign("TheDeveloperBlog tutorial",10);
cout<<"Assigned string is :" <<s;
return 0; 
} 

Output:

Assigned string is : TheDeveloperBlog

Example 4

Let's see simple example when character value is given in a parameter.

#include<iostream>
using namespace std;
int main()
{
string s;
s.assign(10.'a');
cout<<s;
return 0; 
}

Output:

aaaaaaaaaa

Next TopicC++ Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf