0Pricing
C++ Academy · Lesson

String Operations: concat substr find replace

Concatenate, slice, search, and replace string content with the std::string API.

String Operations: concat substr find replace is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Concatenation with +

The + operator joins two strings into a new one. += appends in place.

std::string a = "Hello, ";
std::string b = "World";
std::string c = a + b;     // "Hello, World"
a += b;                    // a is now "Hello, World"

Appending Efficiently

Repeated + on long strings creates many temporaries. Use += or append() for hot loops.

std::string result;
for (auto& word : words) {
    result += word;
    result += ' ';
}

Substring with substr

substr(pos, len) returns a new string starting at pos. len defaults to the rest of the string.

std::string s = "Hello, World";
std::cout << s.substr(0, 5);   // "Hello"
std::cout << s.substr(7);      // "World"

Finding Substrings: find

find(needle) returns the index of the first match, or std::string::npos if not found.

std::string s = "the quick brown fox";
auto pos = s.find("quick");
if (pos != std::string::npos)
    std::cout << "Found at " << pos;

Finding from a Position

Pass a second argument to start searching from a specific index — useful for finding all occurrences.

std::string s = "abc abc abc";
size_t pos = 0;
while ((pos = s.find("abc", pos)) != std::string::npos) {
    std::cout << pos << ' ';
    pos += 3;
}

rfind: Search from the End

rfind finds the last occurrence. find_first_of and find_last_of search for any character from a set.

Replace in Place

replace(pos, len, str) swaps out len characters at pos with str — the new string can have a different length.

std::string s = "Hello, World";
s.replace(7, 5, "C++");
std::cout << s;  // "Hello, C++"

Erase Characters

erase(pos, len) removes a substring. Calling erase() with no arguments clears the whole string.

std::string s = "Hello, World";
s.erase(5, 2);          // removes ", "
std::cout << s;         // "HelloWorld"

Insert Characters

insert(pos, str) inserts str at pos, shifting everything else right.

std::string s = "HelloWorld";
s.insert(5, ", ");
std::cout << s;  // "Hello, World"

Comparing Strings

Use the standard comparison operators or compare(). Comparison is lexicographic.

std::string a = "apple", b = "banana";
if (a < b) std::cout << "apple comes first";

Replacing All Occurrences

The standard library has no single-call replace-all. Write a simple helper or use std::regex.

void replaceAll(std::string& s, const std::string& from, const std::string& to) {
    size_t pos = 0;
    while ((pos = s.find(from, pos)) != std::string::npos) {
        s.replace(pos, from.size(), to);
        pos += to.size();
    }
}

Quick Check

What does "hello".find("xyz") return when the substring is absent?

Recap

std::string offers a rich API: + and += for joining, substr for slicing, find for searching, and replace, erase, insert for editing. Compare with std::string::npos after find.

Frequently asked questions

Is the “String Operations: concat substr find replace” lesson free?

Yes — the full text of “String Operations: concat substr find replace” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “String Operations: concat substr find replace”?

Concatenate, slice, search, and replace string content with the std::string API. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “String Operations: concat substr find replace” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. std::string vs C-Style Char Arrays
  2. String Operations: concat substr find replace
  3. String Stream stringstream for Parsing
  4. String Conversions: to_string and stoi
← Back to C++ Academy