Lightweight String Handling with std::string_view in C++
Learn std::string_view in C++ to perform fast, lightweight string operations without unnecessary string copying.
Introduced in C++17, std::string_view is a lightweight, high-performance string
abstraction that allows working with text without copying it.
Compared to std::string, string_view is much faster for many operations because it does not
allocate or own memory — it simply provides a view over existing character data.
This article explains the advantages of string_view, its usage scenarios, and important caveats.
1) What is string_view?
std::string_view represents all or part of a string without copying it. Internally, it stores only two things:
- A pointer to the beginning of the text
- The length of the text
This means string_view does not own the data — it merely references it.
#include <string_view>
#include <iostream>
using namespace std;
int main() {
string text = "Hello World";
string_view sv = text;
cout << sv << endl;
}
2) Performance Benefits of string_view
std::string → copies data
Copying can be expensive, especially for large strings.
std::string_view → does NOT copy data
It stores only a pointer and a length. This makes it extremely fast and ideal for parsing, log processing, configuration reading, and similar tasks.
3) Substring Extraction with string_view
string_view sv = "C++ Programming Language";
auto first = sv.substr(0, 3); // "C++"
auto second = sv.substr(4, 11); // "Programming"
cout << first << " | " << second;
4) Trimming Whitespace (trim example)
string_view trim(string_view sv) {
while (!sv.empty() && isspace(sv.front()))
sv.remove_prefix(1);
while (!sv.empty() && isspace(sv.back()))
sv.remove_suffix(1);
return sv;
}
int main() {
string_view text = " Hello ";
string_view cleaned = trim(text);
cout << "[" << cleaned << "]";
}
Output: [Hello]
5) Splitting Strings with string_view
#include <vector>
vector<string_view> split(string_view sv, char delimiter) {
vector<string_view> parts;
size_t start = 0;
while (true) {
size_t pos = sv.find(delimiter, start);
if (pos == string_view::npos) {
parts.push_back(sv.substr(start));
break;
}
parts.push_back(sv.substr(start, pos - start));
start = pos + 1;
}
return parts;
}
int main() {
string_view text = "cpp,java,python";
auto parts = split(text, ',');
for (auto p : parts)
cout << p << endl;
}
6) Important Warnings When Using string_view
- Warning: string_view does NOT own the data.
- Thus: If the underlying data is destroyed, string_view becomes invalid.
Dangerous Example
string_view danger() {
string temp = "Hello"; // destroyed at end of function!
return temp; // DANGEROUS!
}
Result: undefined behavior.
Correct Usage
string globalText = "Safe";
string_view sv = globalText; // safe
7) Comparing string_view Values
string_view a = "cpp";
string_view b = "cpp";
if (a == b)
cout << "Equal";
string_view supports direct comparison of content.
8) Practical Example — Command Parsing
void parseCommand(string_view cmd) {
if (cmd.starts_with("add"))
cout << "Add command";
else if (cmd.starts_with("delete"))
cout << "Delete command";
}
int main() {
parseCommand("add user");
}
9) When Should You Use string_view?
- Working with large text data
- Extracting substrings efficiently
- Avoiding unnecessary memory allocations
- Performance-critical parsing tasks
10) TL;DR
- string_view: lightweight, non-owning string type.
- Extremely fast for substring operations.
- Be careful: underlying data must remain valid.
- Ideal for parsing, log processing, config reading.
- All examples work with Visual Studio 2022 and GCC.
Related Articles
Introduction to Functions in C++: Define, Call, Parameters
Learn functions in C++, including defining, calling, and using parameters to build modular and reusable programs.
Overview of the C++ Standard Library
Learn the C++ Standard Library, including STL containers, algorithms, iterators, and essential components for modern C++ development.
String Handling in C++: C-Style and std::string
Learn string handling in C++ using C-style strings and std::string for text processing, concatenation, and manipulation.