std::string
Operations
Master C++ string manipulation: length, substr, find, and concatenation to build powerful text-processing programs.
What is std::string?
In C++, std::string is a class from the <string> header that manages a dynamic sequence of characters. Unlike C-style character arrays, std::string automatically handles memory allocation and resizing.
Strings as Indexed Characters
A std::string stores characters at zero-based indices. Each character can be accessed using operator[] or the .at() method.
Why std::string Over C-Strings?
C++ inherited C-style strings (char arrays), but std::string is safer, more flexible, and far easier to use for modern programming.
The Core Operations
Every C++ programmer must master these four essential string operations. They form the foundation of all text manipulation.
.length() and .size()
Both .length() and .size() return the number of characters in the string. They are identical in behavior: .size() was added for STL container consistency.
Extracting with .substr()
.substr(pos, len) returns a new string starting at index pos with up to len characters. If len is omitted, it extracts through the end.
Searching with .find()
.find(target) searches for a substring or character and returns the index of the first occurrence. If not found, it returns std::string::npos.
Concatenation with + and +=
The + operator joins two strings into a new one. The += operator appends to an existing string in place, which is more efficient for building strings incrementally.
operator[] vs .at()
Both access individual characters by index, but they differ in safety. Understanding when to use each is critical for robust code.
| operator[] | .at() | |
|---|---|---|
| Bounds checking | None | Yes |
| Out-of-range | Undefined behavior | Throws std::out_of_range |
| Performance | Faster | Slight overhead |
| Best for | Validated indices | Untrusted input |
How to Apply String Operations
Follow this checklist when working with strings in any C++ program:
Example 1: Extract a First Name
Given a full name like "Alice Johnson", extract just the first name using .find() and .substr().
Example 2: File Extension Check
Determine if a filename ends with ".cpp" by using .length() and .substr().
Example 3: Build a Sentence
Concatenate words from a vector into a single sentence with spaces between them.
Common Mistakes with Strings
Quick Reference Table
| Operation | Syntax | Returns |
|---|---|---|
| Length | s.length() / s.size() | Number of characters |
| Substring | s.substr(pos, len) | New string |
| Find | s.find(target) | Index or npos |
| Concatenate | s1 + s2 / s1 += s2 | Combined string |
| Access | s[i] / s.at(i) | Character (char) |
| Empty check | s.empty() | bool |
Question 1 of 5
What does s.substr(3, 4) return when s = "Compiler"?
Question 2 of 5
What does s.find("xyz") return when s = "Hello"?
Question 3 of 5
Given std::string a = "Code"; std::string b = a + "!"; what is the value of a after this code runs?
Question 4 of 5
Which code correctly extracts the last name from "Jane Doe"?
Question 5 of 5
What is the key difference between s[10] and s.at(10) when the string has only 5 characters?