string:replace

C++ Reference

replace

Syntax:

    #include <string>
    string& replace( size_type index, size_type num, const string& str );
    string& replace( size_type index1, size_type num1, const string& str, size_type index2, size_type num2 );
    string& replace( size_type index, size_type num, const char* str );
    string& replace( size_type index, size_type num1, const char* str, size_type num2 );
    string& replace( size_type index, size_type num1, size_type num2, char ch);
 
    string& replace( iterator start, iterator end, const string& str );
    string& replace( iterator start, iterator end, const char* str );
    string& replace( iterator start, iterator end, const char* str, size_type num );
    string& replace( iterator start, iterator end, size_type num, char ch );

The function replace() either:

* replaces characters of the current string with up to num characters from str,

  beginning at index,
 
  with up to num2 characters from str beginning at index2,
 
  beginning at index in str,
 
  with num2 characters from str beginning at index2,
 
  with num2 copies of ch,

* replaces characters in the current string from start to end with num

  characters from str,
 
  copies of ch.

For example, the following code displays the string “They say he carved it himself…find your soul-mate, Homer.”

     string s = "They say he carved it himself...from a BIGGER spoon";
     string s2 = "find your soul-mate, Homer.";
     s.replace( 32, s2.length(), s2 );
     cout << s << endl;

Related Topics: insert