Find substring between two indices in C++
You can do this:
std::string(&s[start], &s[end+1])
or this:
std::string(s.c_str() + start, s.c_str() + end + 1)
or this:
std::string(s.begin() + start, s.begin() + end + 1)
These approaches require that end
is less than s.size()
, whereas substr()
does not require that.
Don't complain about the +1
--ranges in C++ are always specified as inclusive begin and exclusive end.