std::basic_string<CharT,Traits,Allocator>::replace (3) - Linux Manuals
std::basic_string<CharT,Traits,Allocator>::replace: std::basic_string<CharT,Traits,Allocator>::replace
Command to display std::basic_string<CharT,Traits,Allocator>::replace
manual in Linux: $ man 3 std::basic_string<CharT,Traits,Allocator>::replace
NAME
std::basic_string<CharT,Traits,Allocator>::replace - std::basic_string<CharT,Traits,Allocator>::replace
Synopsis
basic_string& replace( size_type pos, size_type count, (1)
const basic_string& str );
basic_string& replace( const_iterator first, const_iterator last, (1)
const basic_string& str );
basic_string& replace( size_type pos, size_type count,
const basic_string& str, (until C++14)
size_type pos2, size_type count2 );
basic_string& replace( size_type pos, size_type count,
const basic_string& str, (since C++14)
size_type pos2, size_type count2 = npos );
template< class InputIt >
basic_string& replace( const_iterator first, const_iterator last, (3)
InputIt first2, InputIt last2 );
basic_string& replace( size_type pos, size_type count, (4)
const CharT* cstr, size_type count2 );
basic_string& replace( const_iterator first, const_iterator last, (4)
const CharT* cstr, size_type count2 );
basic_string& replace( size_type pos, size_type count, (5)
const CharT* cstr );
basic_string& replace( const_iterator first, const_iterator last, (2) (5)
const CharT* cstr );
basic_string& replace( size_type pos, size_type count, (6)
size_type count2, CharT ch );
basic_string& replace( const_iterator first, const_iterator last, (6)
size_type count2, CharT ch );
basic_string& replace( const_iterator first, const_iterator last, (7) (since C++11)
std::initializer_list<CharT> ilist );
template < class T >
basic_string& replace( size_type pos, size_type count, (8) (since C++17)
const T& t );
template < class T >
basic_string& replace( const_iterator first, const_iterator last, (8) (since C++17)
const T& t );
template < class T >
basic_string& replace( size_type pos, size_type count, const T& t, (9) (since C++17)
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).
This overload has the same effect as overload (6) if InputIt is an integral type. (until C++11)
This overload only participates in overload resolution if InputIt qualifies as an LegacyInputIterator. (since C++11)
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;. These overloads only participate in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, 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. This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, 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 - 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
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 (max_size())
In any case, if an exception is thrown for any reason, this function has no effect (strong exception guarantee).
(since C++11)
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_2946 C++17 string_view overload causes ambiguity in some cases avoided by making it a template
Example
// Run this code
#include <iostream>
#include <string>
int main()
{
std::string str("The quick brown fox jumps over the lazy dog.");
str.replace(10, 5, "red"); // (5)
str.replace(str.begin(), str.begin() + 3, 1, 'A'); // (6)
std::cout << str << '\n';
}
Output:
A quick red fox jumps over the lazy dog.
Pages related to std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::rend, (3) - std::basic_string<CharT,Traits,Allocator>::rend,
- std::basic_string<CharT,Traits,Allocator>::rend,std::basic_string<CharT,Traits,Allocator>::crend (3) - std::basic_string<CharT,Traits,Allocator>::rend,std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::reserve (3) - std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::resize (3) - std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::rbegin, (3) - std::basic_string<CharT,Traits,Allocator>::rbegin,
- std::basic_string<CharT,Traits,Allocator>::rbegin,std::basic_string<CharT,Traits,Allocator>::crbegin (3) - std::basic_string<CharT,Traits,Allocator>::rbegin,std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rfind (3) - std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::append (3) - std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::assign (3) - std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::at (3) - std::basic_string<CharT,Traits,Allocator>::at
- std::basic_string<CharT,Traits,Allocator>::back (3) - std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::basic_string (3) - std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::begin, (3) - std::basic_string<CharT,Traits,Allocator>::begin,
- std::basic_string<CharT,Traits,Allocator>::begin,std::basic_string<CharT,Traits,Allocator>::cbegin (3) - std::basic_string<CharT,Traits,Allocator>::begin,std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::c_str (3) - std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::capacity (3) - std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::clear (3) - std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::compare (3) - std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::copy (3) - std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::data (3) - std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::empty (3) - std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::end, (3) - std::basic_string<CharT,Traits,Allocator>::end,
- std::basic_string<CharT,Traits,Allocator>::end,std::basic_string<CharT,Traits,Allocator>::cend (3) - std::basic_string<CharT,Traits,Allocator>::end,std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::ends_with (3) - std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::erase (3) - std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::find (3) - std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of (3) - std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_first_of (3) - std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of (3) - std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of (3) - std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::front (3) - std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::get_allocator (3) - std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::insert (3) - std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::max_size (3) - std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::npos (3) - std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::operator+= (3) - std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::operator= (3) - std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[] (3) - std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::operatorbasic_string_view (3) - std::basic_string<CharT,Traits,Allocator>::operatorbasic_string_view
- std::basic_string<CharT,Traits,Allocator>::pop_back (3) - std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::push_back (3) - std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit (3) - std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::size, (3) - std::basic_string<CharT,Traits,Allocator>::size,
- std::basic_string<CharT,Traits,Allocator>::size,std::basic_string<CharT,Traits,Allocator>::length (3) - std::basic_string<CharT,Traits,Allocator>::size,std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::starts_with (3) - std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::substr (3) - std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::swap (3) - std::basic_string<CharT,Traits,Allocator>::swap