String view contains member functions that allows modifying view.

  1. remove_prefix(n): removes n characters from left.
  2. remove_suffix(n): removes n characters from right.

Following program shows an example,

#include <string_view>
#include "../helpers/stdout.h"
 
int main(int argc, char const *argv[])
{
    std::string_view sv{"Williams"};
    print(sv);
 
    print("Removing 2 characters from left.");
    sv.remove_prefix(2);
    print(sv);
 
    print("Removing 2 characters from right.");
    sv.remove_suffix(2);
    print(sv);
 
    print("Restoring string view.");
    sv = "Williams";
    print(sv);
    return 0;
}

sv can be reset to original string by reassigning source string to it.

Using this methods, string view is actually viewing substring of strings and thus they may or may not be null terminated. For example, if sv is viewing substring Will then next character will be i, not null character.

References

  1. https://www.learncpp.com/cpp-tutorial/stdstring_view-part-2/