RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
Join.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <format>
4#include <initializer_list>
5#include <string>
6#include <string_view>
7#include <type_traits>
8#include <vector>
9
10namespace RST::String {
11
15
17 template <typename Container>
18 [[nodiscard]] std::string join(const Container& parts, std::string_view separator = "")
19 {
20 std::string result;
21 bool first = true;
22 for (const auto& part : parts) {
23 if (!first)
24 result += separator;
25 if constexpr (std::is_arithmetic_v<std::decay_t<decltype(part)>>)
26 result += std::format("{}", part);
27 else
28 result += part;
29 first = false;
30 }
31 return result;
32 }
33
35 template <typename T>
36 [[nodiscard]] std::string join(std::initializer_list<T> parts, std::string_view separator = "")
37 {
38 return join(std::vector<T>(parts), separator);
39 }
40
42 [[nodiscard]] std::string joinString(const std::vector<std::string>& stringList, char delimiter = '\0');
44}
String helpers: case, trimming, splitting, joining, replacing, parsing and hashing.
Definition Char.hpp:3
std::string join(const Container &parts, std::string_view separator="")
Joins the elements of parts, strings or numbers, with separator between them.
Definition Join.hpp:18
std::string joinString(const std::vector< std::string > &stringList, char delimiter='\0')
Joins stringList with a one-character delimiter; the default adds none.
std::optional< T > parseNumber(std::string_view text, int base=10) noexcept
Parses text as a number, or returns std::nullopt if it is not entirely one.
Definition Convert.hpp:23