TheDeveloperBlog.com

Home | Contact Us

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

C++ String operator=() function

C++ String operator=() 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 operator=()

This function is used to assign a new value to the string, replacing all the current contents.

Syntax

Consider two strings str1 and str2. Syntax would be:

str1.operator=(str2);
str1.operator=(ch);
str1.operator=(const char* s);

Parameter

str2 : str2 is a string object, whose value is to be moved.

ch : ch is a character value, whose value is to be moved.

s : s is a pointer to the null terminated sequence of characters.

Return value

It returns *this.

Example 1

Let's see a simple example.

#include<iostream>
using namespace std;
int main()
{
string str ="C Programs";
string str1="Java Programs";
str.operator=(str1);
cout<<str;
return 0; 
}

Output:

Java Programs

Example 2

Let's see an another simple example.

#include<iostream>
using namespace std;
int main()
{
string str ="Maths is my favorite subject";
char* s ="Science is my favorite subject";
str.operator=(s);
cout<<str;
return 0; 
}

Output:

Science is my favorite subject

Example 3

Let's see this example.

#include<iostream>
using namespace std;
int main()
{
string str ="java";
char ch='C';
str.operator=(ch);
cout<<str;
return 0; 
}

Output:

C

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