• Source
    1. #include <string>
    2. #include <iostream>
    3.  
    4. using namespace std;
    5.  
    6. int main()
    7. {
    8. string a("abcd efg"); // or a="abcd efg";
    9. string b("xyz ijk");
    10. string c;
    11.  
    12. cout << "String empty: " << c.empty() << endl; // String empty: 1
    13. c = a + b; // concatenation
    14. cout << c << endl; // abcd efgxyz ijk
    15. string d = c;
    16. cout << d << endl <<"\n\n"; // abcd efgxyz ijk
    17.  
    18.  
    19. string g("abc abc abd abc");
    20. cout << "String g: " << g << endl; // String g: abc abc abd abc
    21. cout << "Replace 12,1,\"xyz\",3: " << g.replace(12,1,"xyz",3) << endl; // Replace 12,1,"xyz",3: abc abc abd xyzbc
    22. cout << g.replace(0,3,"xyz",3) << endl; // xyz abc abd xyzbc
    23. cout << g.replace(4,3,"xyz",3) << endl; // xyz xyz abd xyzbc
    24. cout << g.replace(4,3,"ijk",1) << endl; // xyz i abd xyzbc
    25. cout << "Find: " << g.find("abd",1) << endl; // Find: 6
    26. cout << g.find("qrs",1) << endl << "\n\n" ;
    27.  
    28. string h("abc abc abd abc");
    29. cout << "String h: " << h << endl;
    30. cout << "Find \"abc\",0: " << h.find("abc",0) << endl; // Find "abc",0: 0
    31. cout << "Find \"abc\",1: " << h.find("abc",1) << endl; // Find "abc",1: 4
    32.  
    33. return 0;
    34. }
    35.  
    36. /**
    37.  * References:
    38.  * www.cplusplus.com/
    39.  * https://sites.google.com/site/smilitude/
    40.  * */