Skip to content

Feature request: substring_before_first(), substring_after_last(), etc. #44

Description

@alt-graph

It would be nice to have a family of substring extraction functions for situations like this:

auto extension = substring_after_last("readme.txt", ".");
REQUIRE(extension == "txt");

auto facility = substring_before_first("FACILITY/DEVICE/LOCATION/PROPERTY", "/");
REQUIRE(facility == "FACILITY");

The use cases for this functionality are frequent, and writing ad-hoc code for it is clumsy and makes the code hard to read.

To start off, I could imagine the following signatures:

std::string substring_after_first(std::string_view string, std::string_view separator);
std::string substring_after_last(std::string_view string, std::string_view separator);
std::string substring_before_first(std::string_view string, std::string_view separator);
std::string substring_before_last(std::string_view string, std::string_view separator);

We could add _sv versions returning string_view as well.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions