std::basic_string::replace

From cppreference.com
 
 
 
std::basic_string
Member functions
basic_string::basic_string
basic_string::operator=
basic_string::assign
basic_string::get_allocator
Element access
basic_string::at
basic_string::operator[]
basic_string::front (C++11)
basic_string::back (C++11)
basic_string::data
basic_string::c_str
Iterators
basic_string::begin
basic_string::cbegin

(C++11)
basic_string::end
basic_string::cend

(C++11)
basic_string::rbegin
basic_string::crbegin

(C++11)
basic_string::rend
basic_string::crend

(C++11)
Capacity
basic_string::empty
basic_string::size
basic_string::length
basic_string::max_size
basic_string::reserve
basic_string::capacity
basic_string::shrink_to_fit (C++11)
Operations
basic_string::clear
basic_string::insert
basic_string::erase
basic_string::push_back
basic_string::pop_back (C++11)
basic_string::append
basic_string::operator+=
basic_string::compare
basic_string::replace
basic_string::substr
basic_string::copy
basic_string::resize
basic_string::swap
Search
basic_string::find
basic_string::rfind
basic_string::find_first_of
basic_string::find_first_not_of
basic_string::find_last_of
basic_string::find_last_not_of
Constants
basic_string::npos
Non-member functions
operator+
operator==
operator!=
operator<
operator>
operator<=
operator>=
swap(std::basic_string)
operator<<
operator>>
getline
stoi
stol
stoll
(C++11)
(C++11)
(C++11)
stoul
stoull
(C++11)
(C++11)
stof
stod
stold
(C++11)
(C++11)
(C++11)
to_string (C++11)
to_wstring (C++11)
Helper classes
hash<std::string>
hash<std::wstring>
hash<std::u32string>
hash<std::u16string>
(C++11)
 
basic_string& replace( size_type pos, size_type count,

                       const basic_string& str );

basic_string& replace( const_iterator first, const_iterator last,

                       const basic_string& str );
(1)
basic_string& replace( size_type pos, size_type count,

                       const basic_string& str,
                       size_type pos2, size_type count2 );

template< class InputIt >
basic_string& replace( const_iterator first, const_iterator last,

                       InputIt first2, InputIt last2 );
(2)
basic_string& replace( size_type pos, size_type count,

                       const CharT* cstr, size_type count2 );

basic_string& replace( const_iterator first, const_iterator last,

                       const CharT* cstr, size_type count2 );
(3)
basic_string& replace( size_type pos, size_type count,

                       const CharT* cstr );

basic_string& replace( const_iterator first, const_iterator last,

                       const CharT* cstr );
(4)
basic_string& replace( size_type pos, size_type count,

                       size_type count2, CharT ch );

basic_string& replace( const_iterator first, const_iterator last,

                       size_type count2, CharT ch );
(5)
basic_string& replace( const_iterator first, const_iterator last,
                       std::initializer_list<CharT> ilist );
(6) (since C++11)

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 or characters in the range [first2, last2)

3) first count2 charcters of the character string pointed to by cstr

4) null-terminated character string pointed to by cstr

5) count2 copies of character ch

6) characters in the initializer list ilist

Contents

[edit] 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
init - initializer list with the characters to use for replacement
Type requirements
-
InputIt must meet the requirements of InputIterator.

[edit] Return value

*this

[edit] 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)

[edit] 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.