Scroll to navigation

std::basic_string::replace(3) C++ Standard Libary std::basic_string::replace(3)

NAME

std::basic_string::replace - std::basic_string::replace

Synopsis

basic_string& replace( size_type pos, size_type (constexpr since count, (1) C++20) const basic_string& str ); basic_string& replace( const_iterator first, (constexpr since const_iterator last, (2) C++20) const basic_string& str ); basic_string& replace( size_type pos, size_type count,
(until C++14) const basic_string& str,

size_type pos2, size_type count2 ); basic_string& replace( size_type pos, size_type count, (since C++14)
(constexpr since const basic_string& str, C++20)

size_type pos2, size_type count2 = npos ); basic_string& replace( size_type pos, size_type (constexpr since count, (4) C++20) const CharT* cstr, size_type count2 ); basic_string& replace( const_iterator first, (constexpr since const_iterator last, (5) C++20) const CharT* cstr, size_type count2 ); basic_string& replace( size_type pos, size_type (constexpr since count, (6) C++20) const CharT* cstr ); basic_string& replace( const_iterator first, (constexpr since const_iterator last, (7) C++20) const CharT* cstr ); basic_string& replace( size_type pos, size_type (constexpr since count, (8) C++20) size_type count2, CharT ch ); basic_string& replace( const_iterator first, (constexpr since const_iterator last, (3) (9) C++20) size_type count2, CharT ch ); template< class InputIt >

basic_string& replace( const_iterator first, (10) (constexpr since const_iterator last, C++20)

InputIt first2, InputIt last2 ); basic_string& replace( const_iterator first, (since C++11) const_iterator last, (11) (constexpr since std::initializer_list<CharT> ilist ); C++20) template< class StringViewLike >
(since C++17) basic_string& replace( size_type pos, size_type (12) (constexpr since count, C++20)

const StringViewLike& t ); template< class StringViewLike >
(since C++17) basic_string& replace( const_iterator first, (13) (constexpr since const_iterator last, C++20)

const StringViewLike& t ); template< class StringViewLike >

basic_string& replace( size_type pos, size_type (since C++17) count, (14) (constexpr since const StringViewLike& t, C++20)

size_type pos2, size_type count2 = npos );

Replaces the characters in the range [begin() + pos, begin() + std::min(pos + count, size())) or [first, last) with given characters.

1,2) Those characters are replaced with str. 3) Those characters are replaced with a substring [pos2, std::min(pos2 + count2, str.size())) of str. 4,5) Those characters are replaced with the characters in the range [cstr, cstr + count2). If [cstr, cstr + count2) is not a valid range, the behavior is undefined. 6,7) Those characters are replaced with the characters in the range [cstr, cstr + Traits::length(cstr)). 8,9) Those characters are replaced with count2 copies of ch. 10) Those characters are replaced with the characters in the range [first2, last2) as if by replace(first, last, basic_string(first2, last2, get_allocator())). 11) Those characters are replaced with the characters in ilist. 12,13) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then those characters are replaced with the characters from sv. These overloads participate in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false. 14) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then those characters are replaced with the characters from the subview sv.substr(pos2, count2). This overload participates in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.

If [begin(), first) or [first, last) is not a valid range, the behavior is undefined.

Parameters

pos - start of the substring that is going to be replaced count - length of the substring that is going to be replaced first, last - range of characters that is going to be replaced str - string to use for replacement pos2 - start of the substring to replace with count2 - number of characters to replace with cstr - pointer to the character string to use for replacement ch - character value to use for replacement first2, last2 - range of characters to use for replacement ilist - initializer list with the characters to use for replacement t - object (convertible to std::basic_string_view) with the characters to use
for replacement

Type requirements

- InputIt must meet the requirements of LegacyInputIterator.

Return value

*this.

Exceptions

1) Throws std::out_of_range if pos > size(). 3) Throws std::out_of_range if pos > size() or pos2 > str.size(). 4,6,8) Throws std::out_of_range if pos > size(). 12,14) Throws std::out_of_range if pos > size().

If the operation would cause size() to exceed max_size(), throws std::length_error.

If an exception is thrown for any reason, these functions have no effect (strong exception safety guarantee).

Example


This section is incomplete
Reason: no example


Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.


DR Applied to Behavior as published Correct behavior LWG 847 C++98 there was no exception safety added strong exception safety
guarantee guarantee LWG 1323 C++98 the types of first and last were changed to const_iterator
iterator LWG 2946 C++17 overloads (12,13) caused ambiguity avoided by making them templates
in some cases

See also

replace_with_range replaces specified portion of a string with a range of characters (C++23) (public member function) regex_replace replaces occurrences of a regular expression with formatted replacement (C++11) text
(function template) replace replaces all values satisfying specific criteria with another value replace_if (function template)

2026.11.10 http://cppreference.com