Copying Strings : string copy « String « C++






Copying Strings

  
#include <iostream>
#include <string>

using namespace std;

int main( )
{
   string s1( "this is a test" );
   string s1_copy;

   s1_copy = s1;

   string s1_duplicate( s1 );
   cout << "String 1:             " << s1
        << "\nCopy of String 1:     " << s1_copy
        << "\nDuplicate of String1: " << s1_duplicate;

   string s2( "B" );
   cout << "\n\nString 2:  " << s2;

   s1 = s2.substr( 0, 6 );
   cout << s1;
}
  
    
  








Related examples in the same category

1.Copy char array from a string to a char pointer
2.Initializing, Assigning (Copying), and Concatenating Strings Using std::string
3.STL string Instantiation and Copy Techniques