Since C++17 you can use std::string_view
, which was created for sharing part of std::string
without copying
std::cout << std::string_view(pBegin, pEnd - pBegin);
pEnd
must point to one pass the last character to print, like how iterators in C++ work, instead of the last character to print
In older C++ standards boost::string_ref
is an alternative. Newer boost versions also have boost::string_view
with the same semantics as std::string_view
. See Differences between boost::string_ref and boost::string_view
If you use Qt then there's also QStringView
and QStringRef
although unfortunately they're used for viewing QString
which stores data in UTF-16 instead of UTF-8 or a byte-oriented encoding
However if you need to process the string by some functions that require null-terminated string without any external libraries then there's a simple solution
char tmpEnd = *pEnd; // backup the after-end character
*pEnd = '\0';
std::cout << pBegin; // use it as normal C-style string, like dosomething(pBegin);
*pEnd = tmpEnd; // restore the char
In this case make sure that pEnd
still points to an element inside the original array and not one past the end of it