1// appending to string
2#include <iostream>
3#include <string>
4
5int main ()
6{
7 std::string str;
8 std::string str2="Writing ";
9 std::string str3="print 10 and then 5 more";
10
11 // used in the same order as described above:
12 str.append(str2); // "Writing "
13 str.append(str3,6,3); // "10 "
14 str.append("dots are cool",5); // "dots "
15 str.append("here: "); // "here: "
16 str.append(10u,'.'); // ".........."
17 str.append(str3.begin()+8,str3.end()); // " and then 5 more"
18 str.append<int>(5,0x2E); // "....."
19
20 std::cout << str << '\n';
21 return 0;
22}
1// inserting into a string
2#include <iostream>
3#include <string>
4
5int main ()
6{
7 std::string str="to be question";
8 std::string str2="the ";
9 std::string str3="or not to be";
10 std::string::iterator it;
11
12 // used in the same order as described above:
13 str.insert(6,str2); // to be (the )question
14 str.insert(6,str3,3,4); // to be (not )the question
15 str.insert(10,"that is cool",8); // to be not (that is )the question
16 str.insert(10,"to be "); // to be not (to be )that is the question
17 str.insert(15,1,':'); // to be not to be(:) that is the question
18 it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
19 str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
20 str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
21
22 std::cout << str << '\n';
23 return 0;
24}
1#include<iostream>
2#include <string>
3int main() {
4 //"Ever thing inside these double quotes becomes const char array"
5// std::string namee = "Caleb" +"Hello";//This will give error because adding const char array to const char array
6 std::string namee = "Caleb";
7 namee += " Hello";//This will work because adding a ptr to a actual string
8 std::cout << namee << std::endl;// output=>Caleb Hello
9//You can also use the below
10 std::string namee2 = std::string("Caleb")+" Hello";// This will work because constructor will convert const char array to string, adding a ptr to string
11 std::cout << namee2 << std::endl;// output=>Caleb Hello
12 std::cin.get();
13}