Header-only C++20 string utility library modeled on Python's str methods.
#include <strutil/strutil.hpp>
#include <iostream>
int main() {
std::string s = " Hello World ";
std::cout << strutil::Strip(s) << "\n"; // -> "Hello World"
std::cout << strutil::Lower(strutil::Strip(s)) << "\n"; // -> "hello world"
}strutil is a single header with no dependencies beyond the C++20 standard library.
Download strutil/strutil.hpp, into your project and include it:
#include "strutil.hpp"include(FetchContent)
FetchContent_Declare(
strutil
GIT_REPOSITORY https://github.com/<you>/strutil.git
GIT_TAG main
)
FetchContent_MakeAvailable(strutil)
target_link_libraries(your_target PRIVATE strutil)Then:
#include <strutil/strutil.hpp>- C++20 (uses
std::string_view,starts_with/ends_with,if constexpr)
Tests use GoogleTest, fetched automatically via CMake's FetchContent — no manual GTest install needed.
- CMake 3.14+
- A C++20 compiler (MSVC 2019 16.11+, GCC 10+, Clang 12+)
cmake -S . -B build
cmake --build build --config Debugcd build
ctest -C Debug --output-on-failureOr run the test binary directly:
./build/tests/strutil_tests # Linux/macOS
.\build\tests\Debug\strutil_tests.exe # Windows (MSVC multi-config)On Windows, CMake's default Visual Studio generator is multi-config — always pass a matching
--config/-Ctocmake --buildandctest. On Linux/macOS with Makefiles or Ninja this isn't needed, but it doesn't hurt to include.
Capitalize(param)— Capitalizes the first character of a stringLower(param)/Upper(param)— Converts a string to lowercase / uppercaseSwapCase(param)- Swaps the case of each character in a stringTitle(param)- Capitalizes the first character of each word in a stringEqualsIgnoreCase(param)- Checks if two strings match, regardless of case
strutil::Capitalize("helLO"); // -> "Hello"
strutil::Lower("HeLLo"); // -> "hello"
strutil::Upper("hELlO"); // -> "HELLO"
strutil::SwapCase("HellO"); // -> "hELLo"
strutil::Title("heLLo, worLD"); // -> "Hello World"
strutil::EqualsIgnoreCase("HELLO", "hello"); // -> trueCenter(param, length, character)- Centers a string by the specified lengthLJust(param, length, character)- Justifies a string to the left by the specified lengthRJust(param, length, character)- Justifies a string to the right by the specified lengthZFill(param, nZero)- Fills a string with a specified number of "0"s at the beginning
strutil::Center("hello", 7); // -> " hello "
strutil::LJust("hello", 7); // -> "hello "
strutil::RJust("hello", 7); // -> " hello"
strutil::ZFill("1010", 8); // -> "00001010"StartsWith(param, value, start, end)- Returns whether a string starts with a specified valueEndsWith(param, value, start, end)- Returns whether a string ends with a specified valueRemovePrefix(param, value)- Removes the prefix from a stringRemoveSuffix(param, value)- Removes the suffix from a string
strutil::StartsWith("hello world", "hello"); // -> true
strutil::EndsWith("hello world", "world"); // -> true
strutil::RemovePrefix("hello", "he"); // -> "llo"
strutil::RemoveSuffix("hello", "lo"); // -> "helFind(param, value, start, end)- Returns the index of the first occurence of a value (returns npos if not found)RFind(param, value, start, end)- Returns the index of the last occurence of a value (returns npos if not found)Index(param, value, start, end)- Returns the index of the first occurence of a value (throws an exception if not found)RIndex(param, value, start, end)- Returns the index of the last occurence of a value (throws an exception if not found)Contains(param, value, start, end)- Returns whether a string contains a value
strutil::Find("foo bar foo bar", "bar"); // -> 4
strutil::RFind("foo bar foo bar", "bar"); // -> 12
strutil::Index("hello", "foo"); // -> Throws error: Value not found.
strutil::RIndex("foo bar foo", "foo"); // -> 8
strutil::Contains("hello", "ell"); // -> trueIsAlNum(param)- Returns whether all the characters in a string are alphanumericIsAlpha(param)- Returns whether all the characters in a string are in the alphabetIsDigit(param)- Returns whether all the characters in a string are digitsIsLower(param)- Returns whether all the characters in a string are lowercaseIsSpace(param)- Returns whether all the characters in a string are whitespacesIsTitle(param)- Returns whether a string follows the rules of a titleIsUpper(param)- Returns whether all the characters in a string are uppercase
strutil::IsAlNum("123abc"); // -> true
strutil::IsAlpha("abc"); // -> true
strutil::IsDigit("123"); // -> true
strutil::IsLower("hello"); // -> true
strutil::IsSpace(" "); // -> true
strutil::IsTitle("Hello World"); // -> true
strutil::IsUpper("HELLO"); // -> truePartition(param, value)- Creates a vector where the string is parted into three partsRPartition(param, value)- Creates a vector where the string is parted into three parts (finds from right)
strutil::Partition("hello=world"); // -> {"hello", "=", "world"}
strutil::RPartition("a=b=c"); // -> {"a=b", "=", "c"}Split(param, separator, count)- Splits the string at a specified separatorRSplit(param, separator, count)- Splits the string at a specified separator (finds from right)
strutil::Split("a::b::c", "::"); // -> {"a", "b", "c"}
strutil::RSplit("a,b,c,d", ",", 2); // -> {"a,b", "c", "d"}Strip(param, value)- Trims the specified value from the start and end of a stringLStrip(param, value)- Trims the specified value from the start of a stringRStrip(param, value)- Trims the specified value from the end of a string
strutil::Strip(" hello "); // -> "hello"
strutil::LStrip(" hello "); // -> "hello "
strutil::RStrip(" hello "); // -> " hello"Count(param, value, start, end)- Returns the number of occurences of a value in a stringFormat(param, ...)- Formats values into a stringJoin(params, separator)- Joins a vector of strings together with a separatorReplace(param, value1, value2, count)- Replaces all occurences of a value with another valueRepeat(param, count)- Returns a string repeated a specified amount of timesReverse(param)- Returns a reversed string
strutil::Count("foo bar bar", "bar"); // -> 2
strutil::Format("{} + {} = {}", 1, true, 3.0); // -> "1 + true = 3.0"
strutil::Join({"hello", "world"}, " "); // -> "hello world"
strutil::Replace("foo bar bar", "bar", "foo"); // -> "foo foo foo"
strutil::Repeat("hello", 3); // -> "hellohellohello"
strutil::Reverse("hello"); // -> "olleh"IsLower() and IsUpper() only checks the characters that are in the alphabet.
// No letters
strutil::IsLower("123"); // -> false
// Letter included
strutil::IsLower("a123"); // -> true
// No letters
strutil::IsUpper("123"); // -> false
// Letter included
strutil::IsUpper("A123"); // -> trueIsTitle() checks that the first letter of each word is uppercase and all leading letters are lowercase.
// Follows title rule
strutil::IsTitle("Hello World"); // -> true
// Doesn't follow rule (leading letters are uppercase)
strutil::IsTitle("Hello WoRld"); // -> falseZFill() handles signs by keeping them at the front and including them in the total string length.
strutil::ZFill("+50", 4); // -> "+050"
strutil::ZFill("-50", 4); // -> "-050"