Inserting Characters into a string - C++ STL

C++ examples for STL:string

Description

Inserting Characters into a string

Demo Code

#include <iostream>
#include <string>

int main(int argc, const char* argv[]) {
    std::string string1("this is a test");
    std::string string2("tessssst");
    std::string string3("12345678");
    std::string string4("xx");

    std::cout << "Initial strings:\nstring1: " << string1
              << "\nstring2: " << string2 << "\nstring3: " << string3
              << "\nstring4: " << string4 << "\n\n";

    // insert "middle" at location 10 in string1
    string1.insert(10, string2);/*from ww  w  .j  ava  2  s.  c  o  m*/

    // insert "xx" at location 3 in string3
    string3.insert(3, string4, 0, std::string::npos);

    std::cout << "Strings after insert:\nstring1: " << string1
              << "\nstring2: " << string2 << "\nstring3: " << string3
              << "\nstring4: " << string4 << "\n\n";

    return 0;
}

Result


Related Tutorials