Header-only C++17 string utility library. Fast, tested, zero dependencies.
#include "strutil.hpp"
// Split
auto parts = strutil::split("a,b,c", ','); // {"a", "b", "c"}
// Join
auto csv = strutil::join(parts, ", "); // "a, b, c"
// Trim
auto clean = strutil::trim(" hello "); // "hello"
// Case conversion
auto upper = strutil::to_upper("hello"); // "HELLO"
// Replace
auto fixed = strutil::replace("a-b-c", "-", "_"); // "a_b_c"
// Check prefix/suffix
strutil::starts_with("hello world", "hello"); // true
strutil::ends_with("hello world", "world"); // true
// Parse numbers
auto n = strutil::to_int("42"); // std::optional<int>{42}| Function | Description |
|---|---|
split(str, delim) |
Split by character or string |
join(parts, delim) |
Join with delimiter |
trim(str) |
Trim whitespace both sides |
trim_left(str) / trim_right(str) |
Trim one side |
to_upper(str) / to_lower(str) |
Case conversion |
starts_with(str, prefix) |
Prefix check |
ends_with(str, suffix) |
Suffix check |
replace(str, from, to) |
Replace all occurrences |
pad_left(str, width) / pad_right(str, width) |
Padding |
contains(haystack, needle) |
Substring check |
count(str, sub) |
Count occurrences |
slice(str, start, end?) |
Python-style slicing |
to_int(str) / to_double(str) |
Safe number parsing |
mkdir build && cd build
cmake ..
make
make testauto s = strutil::trim(" hi "); // "hi"
auto v = strutil::split("a,b", ','); // {"a","b"}
auto u = strutil::to_upper("hi"); // "HI"
bool b = strutil::starts_with(s, "x"); // false