C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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 1Let'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 2Let'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 3Let'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
|