[string.ops] # 27 Strings library [[strings]](./#strings) ## 27.4 String classes [[string.classes]](string.classes#string.ops) ### 27.4.3 Class template basic_string [[basic.string]](basic.string#string.ops) #### 27.4.3.8 String operations [string.ops] #### [27.4.3.8.1](#string.accessors) Accessors [[string.accessors]](string.accessors) [🔗](#lib:c_str,basic_string) `constexpr const charT* c_str() const noexcept; constexpr const charT* data() const noexcept; ` [1](#string.accessors-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4297) *Returns*: A pointer p such that p + i == addressof(operator[](i)) for eachi in [0, size()][.](#string.accessors-1.sentence-1) [2](#string.accessors-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4302) *Complexity*: Constant time[.](#string.accessors-2.sentence-1) [3](#string.accessors-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4306) *Remarks*: The program shall not modify any of the values stored in the character array; otherwise, the behavior is undefined[.](#string.accessors-3.sentence-1) [🔗](#lib:data,basic_string_) `constexpr charT* data() noexcept; ` [4](#string.accessors-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4317) *Returns*: A pointer p such that p + i == addressof(operator[](i)) for eachi in [0, size()][.](#string.accessors-4.sentence-1) [5](#string.accessors-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4322) *Complexity*: Constant time[.](#string.accessors-5.sentence-1) [6](#string.accessors-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4326) *Remarks*: The program shall not modify the value stored at p + size() to any value other than charT(); otherwise, the behavior is undefined[.](#string.accessors-6.sentence-1) [🔗](#lib:operator_basic_string_view,basic_string) `constexpr operator basic_string_view() const noexcept; ` [7](#string.accessors-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4338) *Effects*: Equivalent to:return basic_string_view(data(), size()); [🔗](#lib:get_allocator,basic_string) `constexpr allocator_type get_allocator() const noexcept; ` [8](#string.accessors-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4350) *Returns*: A copy of theAllocator object used to construct the string or, if that allocator has been replaced, a copy of the most recent replacement[.](#string.accessors-8.sentence-1) #### [27.4.3.8.2](#string.find) Searching [[string.find]](string.find) [1](#string.find-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4360) Let *F* be one offind, rfind, find_first_of, find_last_of,find_first_not_of, and find_last_not_of[.](#string.find-1.sentence-1) - [(1.1)](#string.find-1.1) Each member function of the formconstexpr size_type *F*(const basic_string& str, size_type pos) const noexcept; has effects equivalent to:return *F*(basic_string_view(str), pos); - [(1.2)](#string.find-1.2) Each member function of the formconstexpr size_type *F*(const charT* s, size_type pos) const; has effects equivalent to:return *F*(basic_string_view(s), pos); - [(1.3)](#string.find-1.3) Each member function of the formconstexpr size_type *F*(const charT* s, size_type pos, size_type n) const; has effects equivalent to:return *F*(basic_string_view(s, n), pos); - [(1.4)](#string.find-1.4) Each member function of the formconstexpr size_type *F*(charT c, size_type pos) const noexcept; has effects equivalent to:return *F*(basic_string_view(addressof(c), 1), pos); [🔗](#lib:find,basic_string_) `template constexpr size_type find(const T& t, size_type pos = 0) const noexcept(see below); template constexpr size_type rfind(const T& t, size_type pos = npos) const noexcept(see below); template constexpr size_type find_first_of(const T& t, size_type pos = 0) const noexcept(see below); template constexpr size_type find_last_of(const T& t, size_type pos = npos) const noexcept(see below); template constexpr size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept(see below); template constexpr size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept(see below); ` [2](#string.find-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4429) *Constraints*: - [(2.1)](#string.find-2.1) is_convertible_v> istrue and - [(2.2)](#string.find-2.2) is_convertible_v isfalse[.](#string.find-2.sentence-1) [3](#string.find-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4440) *Effects*: Let *G* be the name of the function[.](#string.find-3.sentence-1) Equivalent to:basic_string_view s = *this, sv = t;return s.*G*(sv, pos); [4](#string.find-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4449) *Remarks*: The exception specification is equivalent tois_nothrow_convertible_v>[.](#string.find-4.sentence-1) #### [27.4.3.8.3](#string.substr) basic_string​::​substr [[string.substr]](string.substr) [🔗](#lib:substr,basic_string) `constexpr basic_string substr(size_type pos = 0, size_type n = npos) const &; ` [1](#string.substr-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4463) *Effects*: Equivalent to: return basic_string(*this, pos, n); [🔗](#lib:substr,basic_string_) `constexpr basic_string substr(size_type pos = 0, size_type n = npos) &&; ` [2](#string.substr-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4474) *Effects*: Equivalent to: return basic_string(std​::​move(*this), pos, n); [🔗](#lib:subview,basic_string) `constexpr basic_string_view subview(size_type pos = 0, size_type n = npos) const; ` [3](#string.substr-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4485) *Effects*: Equivalent to: return basic_string_view(*this).subview(pos, n); #### [27.4.3.8.4](#string.compare) basic_string​::​compare [[string.compare]](string.compare) [🔗](#lib:compare,basic_string) `template constexpr int compare(const T& t) const noexcept(see below); ` [1](#string.compare-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4499) *Constraints*: - [(1.1)](#string.compare-1.1) is_convertible_v> istrue and - [(1.2)](#string.compare-1.2) is_convertible_v isfalse[.](#string.compare-1.sentence-1) [2](#string.compare-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4510) *Effects*: Equivalent to: return basic_string_view(*this).compare(t); [3](#string.compare-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4514) *Remarks*: The exception specification is equivalent tois_nothrow_convertible_v>[.](#string.compare-3.sentence-1) [🔗](#lib:compare,basic_string_) `template constexpr int compare(size_type pos1, size_type n1, const T& t) const; ` [4](#string.compare-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4527) *Constraints*: - [(4.1)](#string.compare-4.1) is_convertible_v> istrue and - [(4.2)](#string.compare-4.2) is_convertible_v isfalse[.](#string.compare-4.sentence-1) [5](#string.compare-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4538) *Effects*: Equivalent to:return basic_string_view(*this).substr(pos1, n1).compare(t); [🔗](#lib:compare,basic_string__) `template constexpr int compare(size_type pos1, size_type n1, const T& t, size_type pos2, size_type n2 = npos) const; ` [6](#string.compare-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4554) *Constraints*: - [(6.1)](#string.compare-6.1) is_convertible_v> istrue and - [(6.2)](#string.compare-6.2) is_convertible_v isfalse[.](#string.compare-6.sentence-1) [7](#string.compare-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4565) *Effects*: Equivalent to:basic_string_view s = *this, sv = t;return s.substr(pos1, n1).compare(sv.substr(pos2, n2)); [🔗](#lib:compare,basic_string___) `constexpr int compare(const basic_string& str) const noexcept; ` [8](#string.compare-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4580) *Effects*: Equivalent to:return compare(basic_string_view(str)); [🔗](#lib:compare,basic_string____) `constexpr int compare(size_type pos1, size_type n1, const basic_string& str) const; ` [9](#string.compare-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4592) *Effects*: Equivalent to:return compare(pos1, n1, basic_string_view(str)); [🔗](#lib:compare,basic_string_____) `constexpr int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2 = npos) const; ` [10](#string.compare-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4605) *Effects*: Equivalent to:return compare(pos1, n1, basic_string_view(str), pos2, n2); [🔗](#lib:compare,basic_string______) `constexpr int compare(const charT* s) const; ` [11](#string.compare-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4619) *Effects*: Equivalent to:return compare(basic_string_view(s)); [🔗](#lib:compare,basic_string_______) `constexpr int compare(size_type pos, size_type n1, const charT* s) const; ` [12](#string.compare-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4631) *Effects*: Equivalent to: return compare(pos, n1, basic_string_view(s)); [🔗](#lib:compare,basic_string________) `constexpr int compare(size_type pos, size_type n1, const charT* s, size_type n2) const; ` [13](#string.compare-13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4642) *Effects*: Equivalent to: return compare(pos, n1, basic_string_view(s, n2)); #### [27.4.3.8.5](#string.starts.with) basic_string​::​starts_with [[string.starts.with]](string.starts.with) [🔗](#lib:starts_with,basic_string) `constexpr bool starts_with(basic_string_view x) const noexcept; constexpr bool starts_with(charT x) const noexcept; constexpr bool starts_with(const charT* x) const; ` [1](#string.starts.with-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4657) *Effects*: Equivalent to:return basic_string_view(data(), size()).starts_with(x); #### [27.4.3.8.6](#string.ends.with) basic_string​::​ends_with [[string.ends.with]](string.ends.with) [🔗](#lib:ends_with,basic_string) `constexpr bool ends_with(basic_string_view x) const noexcept; constexpr bool ends_with(charT x) const noexcept; constexpr bool ends_with(const charT* x) const; ` [1](#string.ends.with-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4675) *Effects*: Equivalent to:return basic_string_view(data(), size()).ends_with(x); #### [27.4.3.8.7](#string.contains) basic_string​::​contains [[string.contains]](string.contains) [🔗](#lib:contains,basic_string) `constexpr bool contains(basic_string_view x) const noexcept; constexpr bool contains(charT x) const noexcept; constexpr bool contains(const charT* x) const; ` [1](#string.contains-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4693) *Effects*: Equivalent to:return basic_string_view(data(), size()).contains(x);