W3cubDocs

/C++

std::basic_string::replace

basic_string& replace( size_type pos, size_type count,
                       const basic_string& str );
(1)
basic_string& replace( const_iterator first, const_iterator last,
                       const basic_string& str );
(1)
(2)
basic_string& replace( size_type pos, size_type count,
                       const basic_string& str,
                       size_type pos2, size_type count2 );
(until C++14)
basic_string& replace( size_type pos, size_type count,
                       const basic_string& str,
                       size_type pos2, size_type count2 = npos );
(since C++14)
template< class InputIt >
basic_string& replace( const_iterator first, const_iterator last,
                       InputIt first2, InputIt last2 );
(3)
basic_string& replace( size_type pos, size_type count,
                       const CharT* cstr, size_type count2 );
(4)
basic_string& replace( const_iterator first, const_iterator last,
                       const CharT* cstr, size_type count2 );
(4)
basic_string& replace( size_type pos, size_type count,
                       const CharT* cstr );
(5)
basic_string& replace( const_iterator first, const_iterator last,
                       const CharT* cstr );
(5)
basic_string& replace( size_type pos, size_type count,
                       size_type count2, CharT ch );
(6)
basic_string& replace( const_iterator first, const_iterator last,
                       size_type count2, CharT ch );
(6)
basic_string& replace( const_iterator first, const_iterator last, 
                       std::initializer_list<CharT> ilist );
(7) (since C++11)
basic_string& replace( size_type pos, size_type count,
                       std::basic_string_view<CharT, Traits> sv );
(8) (since C++17)
basic_string& replace( const_iterator first, const_iterator last,
                       std::basic_string_view<CharT, Traits> sv );
(9) (since C++17)
basic_string& replace( size_type pos, size_type count,
                       std::basic_string_view<CharT, Traits> sv,
                       size_type pos2, size_type count2 = npos );
(10) (since C++17)

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). This overload has the same effect as overload (6) if InputIt is an integral type.
4) first count2 characters of the character string pointed to by cstr;
5) null-terminated character string pointed to by cstr;
6) count2 copies of character ch;
7) characters in the initializer list ilist;
8) characters in the string view sv (equivalent to replace(pos, count, sv.data(), sv.size()))
9) characters in the string view sv (equivalent to replace(first - begin(), last - first, sv))
10) subview [pos2, pos2 + count2) of sv, except if count2==npos or if it would extend past sv.size(), [pos2, sv.size()) is used.

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
sv - std::basic_string_view with the characters to use for replacement

Return value

*this.

Exceptions

std::out_of_range if pos > length() or pos2 > str.length().

std::length_error if the resulting string will exceed maximum possible string length (std::string::npos - 1).

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

Example

#include <iostream>
#include <string>
 
int main()
{
    std::string str("The quick brown fox jumps over the lazy dog.");
 
    str.replace(10, 5, "red"); // (4)
 
    str.replace(str.begin(), str.begin() + 3, 1, 'A'); // (5)
 
    std::cout << str << '\n';
}

Output:

A quick red fox jumps over the lazy dog.

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/string/basic_string/replace