C++ String Library - assign


Advertisements


Description

It appends character c to the end of the string, increasing its length by one.

Declaration

Following is the declaration for std::string::assign.

string& assign (const string& str);

C++11

string& assign (const string& str);

C++14

string& assign (const string& str);

Parameters

  • c − It is a character object.

  • str − It is a string object.

Return Value

It returns *this.

Exceptions

if an exception is thrown, there are no changes in the string.

Example

In below example for std::string::assign.

#include <iostream>
#include <string>

int main () {
   std::string str;
   std::string base="Sairamkrishna is a one of the tech person in tutorialspoint.";

   str.assign(base);
   std::cout << str << '\n';

   str.assign(base,10,9);
   std::cout << str << '\n';

   str.assign("pangrams are cool",7);
   std::cout << str << '\n';

   str.assign("c-string");
   std::cout << str << '\n';

   str.assign(10,'*');
   std::cout << str << '\n';

   str.assign<int>(10,0x2D);
   std::cout << str << '\n';

   str.assign(base.begin()+16,base.end()-12);
   std::cout << str << '\n';

   return 0;
}

The sample output should be like this −

Sairamkrishna is a one of the tech person in tutorialspoint.
hna is a 
pangram
c-string
**********
----------
 a one of the tech person in tut

string.htm

Advertisements