Advertisement
Serafim

Untitled

Aug 29th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. // replacing in a string
  2. #include <iostream>
  3. #include <string>
  4.  
  5. int main ()
  6. {
  7.   std::string base="this is a test string.";
  8.   std::string str2="n example";
  9.   std::string str3="sample phrase";
  10.   std::string str4="useful.";
  11.  
  12.   // replace signatures used in the same order as described above:
  13.  
  14.   // Using positions:                 0123456789*123456789*12345
  15.   std::string str=base;           // "this is a test string."
  16.   str.replace(9,5,str2);          // "this is an example string." (1)
  17.   str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
  18.   str.replace(8,10,"just a");     // "this is just a phrase."     (3)
  19.   str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4)
  20.   str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5)
  21.  
  22.   // Using iterators:                                               0123456789*123456789*
  23.   str.replace(str.begin(),str.end()-3,str3);                    // "sample phrase!!!"      (1)
  24.   str.replace(str.begin(),str.begin()+6,"replace");             // "replace phrase!!!"     (3)
  25.   str.replace(str.begin()+8,str.begin()+14,"is coolness",7);    // "replace is cool!!!"    (4)
  26.   str.replace(str.begin()+12,str.end()-4,4,'o');                // "replace is cooool!!!"  (5)
  27.   str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful."    (6)
  28.   std::cout << str << '\n';
  29.   return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement