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