Skip to content

string

split

  • strtok in C

  • std::string::find + std::string::substr in-place

1
2
3
4
5
6
7
8
9
auto start = 0U;
auto end = s.find(delim);
while (end != std::string::npos)
{
    std::cout << s.substr(start, end - start) << std::endl;
    start = end + delim.length();
    end = s.find(delim, start);
}
std::cout << s.substr(start, end);
  • string_view >= C++17
    • TODO: testing
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
std::vector<std::string_view> split(std::string_view buffer, const std::string_view delimeter = " ")
{
    std::vector<std::string_view> ret{};
    std::decay_t<decltype(std::string_view::npos)> pos{};
    while ((pos = buffer.find(delimeter)) != std::string_view::npos)
    {
        const auto match = buffer.substr(0, pos);
        if (!match.empty())
            ret.push_back(match);
        buffer = buffer.substr(pos + delimeter.size());
    }
    if (!buffer.empty())
        ret.push_back(buffer);
    return ret;
}
  • = C++20

    • TODO: testing
1
2
3
4
5
6
7
#include <ranges>
#include <string_view>
auto split = hello
    | std::ranges::views::split(' ')
    | std::ranges::views::transform([](auto&& str) { 
        return std::string_view(&*str.begin(), std::ranges::distance(str)); 
    });

https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c

contains

  • C++23
1
str.contains("niddle");
  • Boost
1
2
#include <boost/algorithm/string.hpp>
bool b = boost::algorithm::contains(s, t);
  • Vanilla
1
2
3
if (s1.find(s2) != std::string::npos) {
    std::cout << "found!" << '\n';
}

endswith, startswith

TODO