[string.view.ops] # 27 Strings library [[strings]](./#strings) ## 27.3 String view classes [[string.view]](string.view#ops) ### 27.3.3 Class template basic_string_view [[string.view.template]](string.view.template#string.view.ops) #### 27.3.3.8 String operations [string.view.ops] [🔗](#lib:copy,basic_string_view) `constexpr size_type copy(charT* s, size_type n, size_type pos = 0) const; ` [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1180) Let rlen be the smaller of n and size() - pos[.](#1.sentence-1) [2](#2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1183) *Preconditions*: [s, s + rlen) is a valid range[.](#2.sentence-1) [3](#3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1187) *Effects*: Equivalent to traits​::​copy(s, data() + pos, rlen)[.](#3.sentence-1) [4](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1191) *Returns*: rlen[.](#4.sentence-1) [5](#5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1195) *Throws*: out_of_range if pos > size()[.](#5.sentence-1) [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1199) *Complexity*: O(rlen)[.](#6.sentence-1) [🔗](#lib:substr,basic_string_view) `constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const; constexpr basic_string_view subview(size_type pos = 0, size_type n = npos) const; ` [7](#7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1212) Let rlen be the smaller of n and size() - pos[.](#7.sentence-1) [8](#8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1215) *Effects*: Determines rlen, the effective length of the string to reference[.](#8.sentence-1) [9](#9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1219) *Returns*: basic_string_view(data() + pos, rlen)[.](#9.sentence-1) [10](#10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1223) *Throws*: out_of_range if pos > size()[.](#10.sentence-1) [🔗](#lib:compare,basic_string_view) `constexpr int compare(basic_string_view str) const noexcept; ` [11](#11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1234) Let rlen be the smaller of size() and str.size()[.](#11.sentence-1) [12](#12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1237) *Effects*: Determines rlen, the effective length of the strings to compare[.](#12.sentence-1) The function then compares the two strings by calling traits​::​compare(data(), str.data(), rlen)[.](#12.sentence-2) [13](#13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1242) *Returns*: The nonzero result if the result of the comparison is nonzero[.](#13.sentence-1) Otherwise, returns a value as indicated in Table [88](#tab:string.view.compare "Table 88: compare() results")[.](#13.sentence-2) Table [88](#tab:string.view.compare) — compare() results [[tab:string.view.compare]](./tab:string.view.compare) | [🔗](#tab:string.view.compare-row-1)
**Condition** | **Return Value** | | --- | --- | | [🔗](#tab:string.view.compare-row-2)
size() < str.size() | < 0 | | [🔗](#tab:string.view.compare-row-3)
size() == str.size() | 0 | | [🔗](#tab:string.view.compare-row-4)
size() > str.size() | > 0 | [14](#14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1252) *Complexity*: O(rlen)[.](#14.sentence-1) [🔗](#lib:compare,basic_string_view_) `constexpr int compare(size_type pos1, size_type n1, basic_string_view str) const; ` [15](#15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1263) *Effects*: Equivalent to: return substr(pos1, n1).compare(str); [🔗](#lib:compare,basic_string_view__) `constexpr int compare(size_type pos1, size_type n1, basic_string_view str, size_type pos2, size_type n2) const; ` [16](#16) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1275) *Effects*: Equivalent to: return substr(pos1, n1).compare(str.substr(pos2, n2)); [🔗](#lib:compare,basic_string_view___) `constexpr int compare(const charT* s) const; ` [17](#17) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1286) *Effects*: Equivalent to: return compare(basic_string_view(s)); [🔗](#lib:compare,basic_string_view____) `constexpr int compare(size_type pos1, size_type n1, const charT* s) const; ` [18](#18) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1297) *Effects*: Equivalent to: return substr(pos1, n1).compare(basic_string_view(s)); [🔗](#lib:compare,basic_string_view_____) `constexpr int compare(size_type pos1, size_type n1, const charT* s, size_type n2) const; ` [19](#19) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1308) *Effects*: Equivalent to: return substr(pos1, n1).compare(basic_string_view(s, n2)); [🔗](#lib:starts_with,basic_string_view) `constexpr bool starts_with(basic_string_view x) const noexcept; ` [20](#20) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1319) Let rlen be the smaller of size() and x.size()[.](#20.sentence-1) [21](#21) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1322) *Effects*: Equivalent to: return basic_string_view(data(), rlen) == x; [🔗](#lib:starts_with,basic_string_view_) `constexpr bool starts_with(charT x) const noexcept; ` [22](#22) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1333) *Effects*: Equivalent to: return !empty() && traits​::​eq(front(), x); [🔗](#lib:starts_with,basic_string_view__) `constexpr bool starts_with(const charT* x) const; ` [23](#23) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1344) *Effects*: Equivalent to: return starts_with(basic_string_view(x)); [🔗](#lib:ends_with,basic_string_view) `constexpr bool ends_with(basic_string_view x) const noexcept; ` [24](#24) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1355) Let rlen be the smaller of size() and x.size()[.](#24.sentence-1) [25](#25) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1358) *Effects*: Equivalent to:return basic_string_view(data() + (size() - rlen), rlen) == x; [🔗](#lib:ends_with,basic_string_view_) `constexpr bool ends_with(charT x) const noexcept; ` [26](#26) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1372) *Effects*: Equivalent to: return !empty() && traits​::​eq(back(), x); [🔗](#lib:ends_with,basic_string_view__) `constexpr bool ends_with(const charT* x) const; ` [27](#27) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1383) *Effects*: Equivalent to: return ends_with(basic_string_view(x)); [🔗](#lib:contains,basic_string_view) `constexpr bool contains(basic_string_view x) const noexcept; constexpr bool contains(charT x) const noexcept; constexpr bool contains(const charT* x) const; ` [28](#28) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L1396) *Effects*: Equivalent to: return find(x) != npos;