String Algorithms
Search and split efficiently.
Searching with find
find returns the index of a substring or npos if absent, working directly on a view without copying.
#include <iostream>
#include <string_view>
int main() {
std::string_view sv = "key=value";
auto pos = sv.find('=');
std::cout << pos << "\n"; // 3
}Checking npos
Always compare a search result against std::string_view::npos before using it as an index.
#include <iostream>
#include <string_view>
int main() {
std::string_view sv = "abc";
if (sv.find('z') == std::string_view::npos)
std::cout << "not found\n";
}All lessons in this course
- Why string_view
- Creating Views
- Pitfalls and Lifetimes
- String Algorithms