c++ add to string code example
Example 1: append string c++
#include <iostream>
#include <string>
int main ()
{
std::string str;
std::string str2="Writing ";
std::string str3="print 10 and then 5 more";
str.append(str2);
str.append(str3,6,3);
str.append("dots are cool",5);
str.append("here: ");
str.append(10u,'.');
str.append(str3.begin()+8,str3.end());
str.append<int>(5,0x2E);
std::cout << str << '\n';
return 0;
}
Example 2: c++ append a char to a string
#include <iostream>
#include <string>
int main ()
{
std::string name ("John");
std::string family ("Smith");
name += " K. ";
name += family;
name += '\n';
std::cout << name;
return 0;
}
Example 3: string insert c++
#include <iostream>
#include <string>
int main ()
{
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it;
str.insert(6,str2);
str.insert(6,str3,3,4);
str.insert(10,"that is cool",8);
str.insert(10,"to be ");
str.insert(15,1,':');
it = str.insert(str.begin()+5,',');
str.insert (str.end(),3,'.');
str.insert (it+2,str3.begin(),str3.begin()+3);
std::cout << str << '\n';
return 0;
}
Example 4: c++ string concatenation
string first_name = "foo"
string last_name = "bar"
std::cout << first_name + " " + last_name << std::endl;
Example 5: append string to another string c++
#include <iostream>
#include <string>
int main ()
{
std::string str = "Hello";
std::string str2 = " World";
std::cout << str + str2 << std::endl;
return 0;
}
Example 6: appending string in c++
#include<iostream>
#include <string>
int main() {
std::string namee = "Caleb";
namee += " Hello";
std::cout << namee << std::endl;
std::string namee2 = std::string("Caleb")+" Hello";
std::cout << namee2 << std::endl;
std::cin.get();
}