std::basic_string_view<CharT,Traits>:: substr
|
constexpr
basic_string_view substr
(
size_type pos
=
0
,
size_type count = npos ) const ; |
(seit C++17) | |
Gibt eine Ansicht des Teilstrings
[
pos
,
pos
+
rlen
)
zurück, wobei
rlen
der kleinere Wert von
count
und
size
(
)
-
pos
ist.
Inhaltsverzeichnis |
Parameter
| pos | - | Position des ersten Zeichens |
| count | - | angeforderte Länge |
Rückgabewert
Ansicht des Teilstrings
[
pos
,
pos
+
rlen
)
.
Exceptions
std::out_of_range wenn pos > size ( ) .
Komplexität
Konstante.
Beispiel
#include <cstddef> #include <iostream> #include <stdexcept> #include <string_view> int main() { typedef std::size_t count_t, pos_t; constexpr std::string_view data{"ABCDEF"}; std::cout << data.substr() << '\n'; // ABCDEF, d.h. data[0, 5] also [0, 6) std::cout << data.substr(pos_t(1)) << '\n'; // BCDEF, d.h. [1, 6) std::cout << data.substr(pos_t(2), count_t(3)) << '\n'; // CDE, d.h. [2, 2 + 3) std::cout << data.substr(pos_t(4), count_t(42)) << '\n'; // EF, d.h. [4, 6) try { [[maybe_unused]] auto sub = data.substr(pos_t(666), count_t(1)); // wirft Exception: pos > size() { catch (std::out_of_range const& ex) { std::cout << ex.what() << '\n'; } }
Mögliche Ausgabe:
ABCDEF BCDEF CDE EF basic_string_view::substr: __pos (which is 666) > __size (which is 6)
Siehe auch
|
kopiert Zeichen
(öffentliche Elementfunktion) |
|
|
findet Zeichen in der View
(öffentliche Elementfunktion) |
|
|
gibt einen Teilstring zurück
(öffentliche Elementfunktion von
std::basic_string<CharT,Traits,Allocator>
)
|