std::string replace() method
- since C++20
- since C++17
- since C++14
- since C++11
- until C++11
// (1) Non const version only
constexpr basic_string& replace( size_type pos, size_type count,
const basic_string& str );
// (1) Non const version only
constexpr basic_string& replace( const_iterator first, const_iterator last,
const basic_string& str );
// (2) Non const version only
constexpr basic_string& replace( size_type pos, size_type count,
const basic_string& str,
size_type pos2, size_type count2 = npos );
// (3) Non const version only
template< class InputIt >
constexpr basic_string& replace( const_iterator first, const_iterator last,
InputIt first2, InputIt last2 );
// (4) Non const version only
constexpr basic_string& replace( size_type pos, size_type count,
const CharT* cstr, size_type count2 );
// (4) Non const version only
constexpr basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr, size_type count2 );
// (5) Non const version only
constexpr basic_string& replace( size_type pos, size_type count,
const CharT* cstr );
// (5) Non const version only
constexpr basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr );
// (6) Non const version only
constexpr basic_string& replace( size_type pos, size_type count,
size_type count2, CharT ch );
// (6) Non const version only
constexpr basic_string& replace( const_iterator first, const_iterator last,
size_type count2, CharT ch );
// (7) Non const version only
constexpr basic_string& replace( const_iterator first, const_iterator last,
std::initializer_list<CharT> ilist );
// (8) Non const version only
template < class StringViewLike >
constexpr basic_string& replace( size_type pos, size_type count,
const StringViewLike& t );
// (8) Non const version only
template < class StringViewLike >
constexpr basic_string& replace( const_iterator first, const_iterator last,
const StringViewLike& t );
// (9) Non const version only
template < class StringViewLike >
constexpr basic_string& replace( size_type pos, size_type count, const StringViewLike& t,
size_type pos2, size_type count2 = npos );
// (1) Non const version only
basic_string& replace( size_type pos, size_type count,
const basic_string& str );
// (1) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const basic_string& str );
// (2) Non const version only
basic_string& replace( size_type pos, size_type count,
const basic_string& str,
size_type pos2, size_type count2 = npos );
// (3) Non const version only
template< class InputIt >
basic_string& replace( const_iterator first, const_iterator last,
InputIt first2, InputIt last2 );
// (4) Non const version only
basic_string& replace( size_type pos, size_type count,
const CharT* cstr, size_type count2 );
// (4) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr, size_type count2 );
// (5) Non const version only
basic_string& replace( size_type pos, size_type count,
const CharT* cstr );
// (5) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr );
// (6) Non const version only
basic_string& replace( size_type pos, size_type count,
size_type count2, CharT ch );
// (6) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
size_type count2, CharT ch );
// (7) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
std::initializer_list<CharT> ilist );
// (8) Non const version only
template < class StringViewLike >
basic_string& replace( size_type pos, size_type count,
const StringViewLike& t );
// (8) Non const version only
template < class StringViewLike >
basic_string& replace( const_iterator first, const_iterator last,
const StringViewLike& t );
// (9) Non const version only
template < class StringViewLike >
basic_string& replace( size_type pos, size_type count, const StringViewLike& t,
size_type pos2, size_type count2 = npos );
// (1) Non const version only
basic_string& replace( size_type pos, size_type count,
const basic_string& str );
// (1) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const basic_string& str );
// (2) Non const version only
basic_string& replace( size_type pos, size_type count,
const basic_string& str,
size_type pos2, size_type count2 = npos );
// (3) Non const version only
template< class InputIt >
basic_string& replace( const_iterator first, const_iterator last,
InputIt first2, InputIt last2 );
// (4) Non const version only
basic_string& replace( size_type pos, size_type count,
const CharT* cstr, size_type count2 );
// (4) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr, size_type count2 );
// (5) Non const version only
basic_string& replace( size_type pos, size_type count,
const CharT* cstr );
// (5) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr );
// (6) Non const version only
basic_string& replace( size_type pos, size_type count,
size_type count2, CharT ch );
// (6) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
size_type count2, CharT ch );
// (7) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
std::initializer_list<CharT> ilist );
// (1) Non const version only
basic_string& replace( size_type pos, size_type count,
const basic_string& str );
// (1) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const basic_string& str );
// (2) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const basic_string& str );
// (3) Non const version only
template< class InputIt >
basic_string& replace( const_iterator first, const_iterator last,
InputIt first2, InputIt last2 );
// (4) Non const version only
basic_string& replace( size_type pos, size_type count,
const CharT* cstr, size_type count2 );
// (4) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr, size_type count2 );
// (5) Non const version only
basic_string& replace( size_type pos, size_type count,
const CharT* cstr );
// (5) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr );
// (6) Non const version only
basic_string& replace( size_type pos, size_type count,
size_type count2, CharT ch );
// (6) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
size_type count2, CharT ch );
// (7) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
std::initializer_list<CharT> ilist );
// (1) Non const version only
basic_string& replace( size_type pos, size_type count,
const basic_string& str );
// (1) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const basic_string& str );
// (2) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const basic_string& str );
// (3) Non const version only
template< class InputIt >
basic_string& replace( const_iterator first, const_iterator last,
InputIt first2, InputIt last2 );
// (4) Non const version only
basic_string& replace( size_type pos, size_type count,
const CharT* cstr, size_type count2 );
// (4) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr, size_type count2 );
// (5) Non const version only
basic_string& replace( size_type pos, size_type count,
const CharT* cstr );
// (5) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr );
// (6) Non const version only
basic_string& replace( size_type pos, size_type count,
size_type count2, CharT ch );
// (6) Non const version only
basic_string& replace( const_iterator first, const_iterator last,
size_type count2, CharT ch );
Replaces the part of the string indicated by either [ pos, pos + count ) or [ first, last ) with a new string.
The new string can be one of:
-
(1) String
str
. -
(2) Substring [ pos2, pos2 + count2 ) of
str
, except ifcount2 == npos
or if would extend paststr.size()
,[ pos2, str.size() )
is used. -
(3) Characters in the range [ first2, last2 ).
- since C++11
- until C++11
Overload ResolutionThis overload only participates in overload resolution if
InputIt
qualifies as anLegacyInputIterator
.This overload has the same effect as overload (6) if
InputIt
is an integral type. -
(4) Characters in the range [ cstr, cstr + count2 ).
-
(5) Characters in the range [ cstr, cstr + Traits::length(cstr) ).
-
(6)
count2
copies of characterch
. -
(7) Characters in the initializer list
ilist
. -
(8) Characters of a string view
sv
, converted fromt
as if bystd::basic_string_view<CharT, Traits> sv = t;
.Overload ResolutionThese overloads participate in overload resolution only if
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>
istrue
andstd::is_convertible_v<const StringViewLike&, const CharT*>
isfalse
-
(9) Subview [ pos2, pos2 + count2 ) of a string view
sv
, converted fromt
as if bystd::basic_string_view<CharT, Traits> sv = t;
, except ifcount2 == npos
or if it would extend pastsv.size()
, [ pos2, sv.size() ) is used.Overload ResolutionThis overload participates in overload resolution only if
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>
istrue
andstd::is_convertible_v<const StringViewLike&, const CharT*>
isfalse
.
:::
Parameters
pos
- start of the substring that is going to be replacedcount
- length of the substring that is going to be replacedfirst
,last
- range of characters that is going to be replacedstr
- string to use for replacementpos2
- start of the substring to replace withcount2
- number of characters to replace withcstr
- pointer to the character string to use for replacementch
- character value to use for replacementfirst2
,last2
- range of characters to use for replacementilist
-std::initializer_list
with the characters to use for replacementt
- object (convertible tostd::basic_string_view
) with the characters to use for replacement
Return value
*this