Skip to content

[cpp,c++ utxcpp] using utx::split_view, (and utx::print, utx::string)

utx::split_view is a class defined in utxcpp library, it splits string into several parts by given delim.

  • Utxcpp is released under the Boost Software License.
  • Publishing software under the Boost Software License doesn't mean the library must use boost libraries.

utx::split_view doc:

• utx::split_view | • utx::string | • utx::print | • utxcpp


class utx::split_view is used to split string into several parts by given delim.


utx::split_view is a class, which allows only one constructor.

const utx::string str1{"Hello, Utxcpp!"};
utx::split_view sv1{str1, ' '};

Split the string str1 with a space: ' '.

re-split method: .split(delim)

The string is splitted on constructing, however you can re-split it by calling .split method.



If you want to get how many parts the string is splitted into, you can use the method .split_size().

// or
std::cout << sv1.split_size() << '\n';

Access each splitted part

You can get each splitted part by calling .operator[], or by iterators.

// operator[]
utx::print(sv1[0], sv1[1]);

// iterators.
for (std::string_view each: sv1)
    utx::print("=>", each);

Get the underlying string

There are two methods to get the underlying string data: .data() and .data_view()

Method .data() returns string which has string copy.

Method .data_view() returns string_view which has no string copy.


At last, I post the complete c++ code example

(utx::rt_assert will throw an exception if the test condition is false.)

#include <utxcpp/core.hpp>

using namespace utx::string_literals;

int main() try {
        // Make strings.
        const utx::string str1{"Hello, Utxcpp!"};       // utx::string
        const auto str2 = "cpp,c++ and utxcpp"_us;      // utx::string
        const auto str3 = """boost ", "software ", 123);        // utx::string
        const auto str4 = "cpp,c++ is powerful and useful."s;   // std::string

        // Print them
        utx::print(str1, "\n", str2, '\n', str3, "\n", str4);

        utx::split_view sv1{str1, ' '}; // create split_view and split with a space: ' '
        utx::rt_assert(sv1.split_size()==2);    // method: .split_size
        utx::print(sv1[0], "=>", sv1[1]);    // Hello, => Utxcpp!

        sv1.split("!"); // re-split with '!'
        utx::print(sv1.split_size());   // 1,    because there is nothing after '!'
        utx::print(sv1[0]);     // Hello, Utxcpp

        sv1.split('o'); // re-split with 'o'
        utx::print(sv1); // {"Hell"; ", Utxcpp!"; }
        std::cout << sv1 << '\n'; // {"Hell"; ", Utxcpp!"; }
        for (std::string_view each: sv1)
                utx::print("=>", each);
        utx::print(; // Hello, Utxcpp!
        utx::print(sv1.data_view()); // Hello, Utxcpp!

        utx::print(sv1.all_data_size()); // 15

} catch (std::exception & exc) {
        utx::printe("[std::exception]", exc.what());
        return 1;

utx::split_view doc:

Utxcpp docs:





No Trackbacks


Display comments as Linear | Threaded

No comments

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.

Form options

Submitted comments will be subject to moderation before being displayed.