Skip to main content

std::string replace() method

// (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 );

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 if count2 == npos or if would extend past str.size(), [ pos2, str.size() ) is used.

  • (3) Characters in the range [ first2, last2 ).

    Overload Resolution

    This overload only participates in overload resolution if InputIt qualifies as an LegacyInputIterator.

  • (4) Characters in the range [ cstr, cstr + count2 ).

  • (5) Characters in the range [ cstr, cstr + Traits::length(cstr) ).

  • (6) count2 copies of character ch.

  • (7) Characters in the initializer list ilist.

  • (8) Characters of a string view sv, converted from t as if by std::basic_string_view<CharT, Traits> sv = t;.

    Overload Resolution

    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

  • (9) Subview [ pos2, pos2 + count2 ) of a string view sv, converted from t as if by std::basic_string_view<CharT, Traits> sv = t;, except if count2 == npos or if it would extend past sv.size(), [ pos2, sv.size() ) is used.

    Overload Resolution

    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.

:::

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 - std::initializer_list with the characters to use for replacement
  • t - object (convertible to std::basic_string_view) with the characters to use for replacement

Return value

*this

Complexity

important

This section requires improvement. You can help by editing this doc page.

Exceptions

In any case, if an exception is thrown for any reason, this function has no effect (strong exception guarantee).  (since C++11)

Example

Main.cpp
#include <cassert>
#include <iterator>
#include <string>

using namespace std::string_literals;

int main()
{
std::string s = "xmplr";

// replace(size_type index, size_type count, char ch)
s.replace(0, 1, 'E');
assert("Exmplr" == s);

// replace(size_type index, const char* s)
s.replace(2, "e");
assert("Exemplr" == s);

// replace(size_type index, string const& str)
s.replace(6, "a"s);
assert("Exemplar" == s);

// replace(size_type index, string const& str,
// size_type index_str, size_type count)
s.replace(8, " is an example string."s, 0, 14);
assert("Exemplar is an example" == s);

// replace(const_iterator pos, char ch)
s.replace(s.cbegin() + s.find_first_of('n') + 1, ':');
assert("Exemplar is an: example" == s);

// replace(const_iterator pos, size_type count, char ch)
s.replace(s.cbegin() + s.find_first_of(':') + 1, 2, '=');
assert("Exemplar is an:== example" == s);

// replace(const_iterator pos, InputIt first, InputIt last)
{
std::string seq = " string";
s.replace(s.begin() + s.find_last_of('e') + 1,
std::begin(seq), std::end(seq));
assert("Exemplar is an:== example string" == s);
}

// replace(const_iterator pos, std::initializer_list<char>)
s.replace(s.cbegin() + s.find_first_of('g') + 1, { '.' });
assert("Exemplar is an:== example string." == s);
}
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::string replace() method

// (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 );

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 if count2 == npos or if would extend past str.size(), [ pos2, str.size() ) is used.

  • (3) Characters in the range [ first2, last2 ).

    Overload Resolution

    This overload only participates in overload resolution if InputIt qualifies as an LegacyInputIterator.

  • (4) Characters in the range [ cstr, cstr + count2 ).

  • (5) Characters in the range [ cstr, cstr + Traits::length(cstr) ).

  • (6) count2 copies of character ch.

  • (7) Characters in the initializer list ilist.

  • (8) Characters of a string view sv, converted from t as if by std::basic_string_view<CharT, Traits> sv = t;.

    Overload Resolution

    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

  • (9) Subview [ pos2, pos2 + count2 ) of a string view sv, converted from t as if by std::basic_string_view<CharT, Traits> sv = t;, except if count2 == npos or if it would extend past sv.size(), [ pos2, sv.size() ) is used.

    Overload Resolution

    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.

:::

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 - std::initializer_list with the characters to use for replacement
  • t - object (convertible to std::basic_string_view) with the characters to use for replacement

Return value

*this

Complexity

important

This section requires improvement. You can help by editing this doc page.

Exceptions

In any case, if an exception is thrown for any reason, this function has no effect (strong exception guarantee).  (since C++11)

Example

Main.cpp
#include <cassert>
#include <iterator>
#include <string>

using namespace std::string_literals;

int main()
{
std::string s = "xmplr";

// replace(size_type index, size_type count, char ch)
s.replace(0, 1, 'E');
assert("Exmplr" == s);

// replace(size_type index, const char* s)
s.replace(2, "e");
assert("Exemplr" == s);

// replace(size_type index, string const& str)
s.replace(6, "a"s);
assert("Exemplar" == s);

// replace(size_type index, string const& str,
// size_type index_str, size_type count)
s.replace(8, " is an example string."s, 0, 14);
assert("Exemplar is an example" == s);

// replace(const_iterator pos, char ch)
s.replace(s.cbegin() + s.find_first_of('n') + 1, ':');
assert("Exemplar is an: example" == s);

// replace(const_iterator pos, size_type count, char ch)
s.replace(s.cbegin() + s.find_first_of(':') + 1, 2, '=');
assert("Exemplar is an:== example" == s);

// replace(const_iterator pos, InputIt first, InputIt last)
{
std::string seq = " string";
s.replace(s.begin() + s.find_last_of('e') + 1,
std::begin(seq), std::end(seq));
assert("Exemplar is an:== example string" == s);
}

// replace(const_iterator pos, std::initializer_list<char>)
s.replace(s.cbegin() + s.find_first_of('g') + 1, { '.' });
assert("Exemplar is an:== example string." == s);
}
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.