openframeworks / openFrameworks

openFrameworks is a community-developed cross platform toolkit for creative coding in C++.
http://openframeworks.cc
Other
9.97k stars 2.55k forks source link

feature: string starts and ends with functions #7054

Open thomasgeissl opened 2 years ago

thomasgeissl commented 2 years ago

I find myself always adding little string helper functions. I think startsWith and endsWith will be added for cpp20.

does it make sense to add something like this to the core?

//ofEndsWith
static bool ofDoesStringEndWith(const std::string &str, const std::string &suffix)
{
    return str.size() >= suffix.size() && 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
}
//ofStartsWith
static bool ofDoesStringStartWith(const std::string &str, const std::string &prefix)
{
    return str.size() >= prefix.size() && 0 == str.compare(0, prefix.size(), prefix);
}

t

oxillo commented 2 years ago

I would find these functions very useful. I would rather use ofStringStartsWith and ofStringEndsWith for the names.

Implementation may use C++20 starts_with() and ends_with() when available.