C++

C++ String View

Index counting of string characters begins from zero (not one). A string range is a continuous sequence of characters from a string literal. The whole string is one big range. Smaller ranges can be obtained from a string. The idea of a string view is to obtain a decided range from a string, such that the range is like an independent string whose own the first character is at index zero. Though a string-view object can stand alone, it is officially a range view and cannot be changed ordinarily.

String View is a class of its own library that should be included in a C++ program as follows:

    #include <string_view>

It has member functions that have been categorized under the following headings: construction, capacity, Element Access, Iterator Support, Modifiers, String Operations, and Searching. Strangely, string_view has modifiers, which conflicts with its definition. That is clarified below in this tutorial. All string view code in this tutorial is in the C++ main() function.

Article Content

Construction
basic_string_view(const charT* str)

This constructs a string-view, from a whole string literal, of constant-pointer-to-characters. The following, illustrates this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* str = "one two three four five";

        string_view strV(str);

        cout <<strV <<endl;

 
        return 0;

    }

The output is:

    one two three four five

Notice that string_view, instead of basic_string_view, has been used for the constructor name.

basic_string_view(const charT* str, size_type len)

This member function windows the first len characters of the string argument. Windowing is not copying. No string-view constructor copies. They just reference. The following program shows this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* str = "one two three four five";

        string_view strV(str, 13);

        cout <<strV <<endl;

 
        return 0;

    }

The output is:

    one two three

basic_string_view(const basic_string_view&)

This constructor is the same as the first constructor above, but its argument is a string_view object, which already had its view. The following program illustrates this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* str = "one two three four five";

        string_view strV(str, 13);

        string_view strVV(strV);

        cout <<strVV <<endl;

 
        return 0;

    }

The output is:

    one two three

basic_string_view& operator=(const basic_string_view&)

This member constructor is similar to the above, but the string_view is assigned and not passed as an argument. The following program illustrates this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* str = "one two three four five";

        string_view strV(str, 13);

        string_view strVV = strV;

        cout <<strVV <<endl;

 
        return 0;

    }

The output is:

    one two three

Capacity
size_type size() const

Remember, a string_view is a range from some original string. It is not a copy of the range. It has references to the character elements of the range in the original string. The size of this string_view (window view) can still be determined because the string_view is an object instantiated from the string_view class. The size is returned by the size() member function. The following program demonstrates this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* str = "one two three four five";

        string_view strV(str, 13);

        int sz = strV.size();

        cout <<sz <<endl;

 
        return 0;

    }

The output is:

    13

bool empty() const

A string_view object can be created empty, as shown in the following program. When empty, the empty() member function will return true; otherwise it will return false. The program is:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        string_view strV;

        bool bl = strV.empty();


        cout <<bl <<endl;


        return 0;

    }

The output is 1, for true. Note that the string_view object was created without parentheses and without any argument.

Element Access
const_reference operator[](size_type pos) const

The square bracket operator can be used to read a value (character) in the string_view. The following program uses a for-loop to illustrate this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* str = "one two three four five";

        string_view strV(str, 13);


        for (int i=0; i <strV.size(); i++)

            cout <<strV[i];

        cout <<endl;


        return 0;

    }

The output is:

    one two three

Note: A character in a string_view object cannot be changed. Any possible change should be done in the original string.

Iterator Support
const_iterator begin() const

This member function returns a constant iterator that points to the first character element of the string_view object. Here, const_iterator means that the character cannot be changed. However, the iterator can be incremented to point to the next character element; or add an integer similar to adding an integer to an index to point to another character element ahead. The following program illustrates this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* str = "one two three four five";

        string_view strV(str, 13);


        string_view::const_iterator it = strV.begin();

        it++;

        it = it + 4;

     
        cout <<*it <<endl;


        return 0;

    }

The output is ‘w’. Note that way the iterator has been constructed.

const_iterator end() const

This member function returns a constant iterator that points just after the last character element of the string_view object. The iterator can be decremented to point to the last character element or be subtracted an integer, similarly to subtracting an integer from an index, to point to another character element in before. Here, const_iterator means that the character cannot be changed. The following program illustrates this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* str = "one two three four five";

        string_view strV(str, 13);


        string_view::const_iterator it = strV.end();

        it--;

        it = it - 4;

     
        cout <<*it <<endl;


        return 0;

    }

The output is ‘t’.

Modifiers
void remove_prefix(size_type n)

This removes a first sub-string from the string_view, but it does not remove any character from the original string. The following program illustrates this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* str = "one two three four five";

        string_view strV(str, 13);


        cout <<strV <<endl;


        strV.remove_prefix(4);

     
        cout <<strV <<endl;

        cout <<str <<endl;


        return 0;

    }

The output is:

    one two three

    two three

    one two three four five

void remove_suffix(size_type n)

This removes a last sub-string from the string_view, but it does not remove any character from the original string. The following program illustrates this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* str = "one two three four five";

        string_view strV(str, 13);


        cout <<strV <<endl;


        strV.remove_suffix(6);

     
        cout <<strV <<endl;

        cout <<str <<endl;


        return 0;

    }

The output is:

    one two three

    one two

    one two three four five

String Operations
size_type copy(charT* s, size_type n, size_type pos = 0) const

This function copies a range of characters from the string_view object (not the original string) to replace the first sub-string of another string. The following program illustrates this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        char strB[] = "aaa bbb ccc ddd eee";

        const char* str = "one two three four five";

        string_view strV(str, 13);

        cout <<strV <<endl;


        strV.copy(strB, 3, 4);

     
        cout <<strB <<endl;

        cout <<str <<endl;


        return 0;

    }

The output is:

    one two three

    two bbb ccc ddd eee

    one two three four five

There are two independent strings here. The string_view is not an independent string. It is a range window of one of the independent strings. Note that the member function’s first parameter, “charT* s” means an array-of-chars and not a const-char* .

int compare(basic_string_view s) const

The number of characters of two string_views is compared here. If the one, using the member function compare(), is higher, then a positive number is returned. If the numbers are equal, then zero is returned. If the string_view object that employs the member function has a lower number, a negative number is returned. The following program illustrates this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* strA = "one two three four five";

        string_view strVA(strA, 13);

        cout <<strVA <<endl;


        const char* strB = "aaa bbb ccc ddd eee";

        string_view strVB(strB, 11);

        cout <<strVB <<endl;


        int ret = strVB.compare(strVA);


        cout <<ret <<endl;


        return 0;

    }

The output is:

    one two three

    aaa bbb ccc

    -14, at the author’s computer.

Searching
size_type find(const charT* s, size_type pos = 0) const

This member function looks for the occurrence of the first sub-string, s in the string_view in question. It returns the index of the first character of the sub-string found. If not found, it returns -1. The second argument tells where the search starts (default index is 0). The following program illustrates this:

    #include <iostream>

    #include <string_view>

    using namespace std;

   
    int main()

    {

        const char* str = "one two three four five";

        string_view strV(str, 13);

        cout <<strV <<endl;


        const char* cs = "two";

        int ret = strV.find(cs, 0);


        cout <<ret <<endl;


        return 0;

    }

The output is:

    one two three

    4

Conclusion

A string_view is a window view of a range of an original string. It is not a copy of the characters. The characters are referred to. There is a string view class from which string_view objects are instantiated. String-View has many member functions. The basic member functions from the different categories have been explained above.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.