Scroll to navigation

std::basic_string::replace(3) C++ Standard Libary std::basic_string::replace(3)

NAME

std::basic_string::replace - std::basic_string::replace

Synopsis

basic_string& replace( size_type pos, size_type (until count, C++20) const basic_string& str ); constexpr basic_string& replace( size_type pos, (since size_type count, C++20) const basic_string& str ); basic_string& replace( const_iterator first, (until const_iterator last, C++20) const basic_string& str ); constexpr basic_string& replace( const_iterator (since first, const_iterator last, C++20) const basic_string& str ); basic_string& replace( size_type pos, size_type count,
(until const basic_string& str, C++14)

size_type pos2, size_type count2 ); basic_string& replace( size_type pos, size_type count, (since
C++14) const basic_string& str, (until
C++20) size_type pos2, size_type count2 = npos ); constexpr basic_string& replace( size_type pos, size_type count,
(since const basic_string& str, C++20)

size_type pos2, size_type count2 = npos ); template< class InputIt >

basic_string& replace( const_iterator first, (until const_iterator last, C++20)

InputIt first2, InputIt last2 ); template< class InputIt >

constexpr basic_string& replace( const_iterator (since first, const_iterator last, C++20)

InputIt first2, InputIt last2 ); basic_string& replace( size_type pos, size_type (until count, C++20) const CharT* cstr, size_type count2 ); constexpr basic_string& replace( size_type pos, (since size_type count, C++20) const CharT* cstr, size_type count2 ); basic_string& replace( const_iterator first, (until const_iterator last, C++20) const CharT* cstr, size_type count2 ); constexpr basic_string& replace( const_iterator (since first, const_iterator last, C++20) const CharT* cstr, size_type count2 ); basic_string& replace( size_type pos, size_type (until count, C++20) const CharT* cstr ); constexpr basic_string& replace( size_type pos, (since size_type count, C++20) const CharT* cstr ); basic_string& replace( const_iterator first, (until const_iterator last, (1) C++20) const CharT* cstr ); constexpr basic_string& replace( const_iterator (since first, const_iterator last, (1) C++20) const CharT* cstr ); basic_string& replace( size_type pos, size_type (until count, (2) C++20) size_type count2, CharT ch ); constexpr basic_string& replace( size_type pos, (since size_type count, C++20) size_type count2, CharT ch ); basic_string& replace( const_iterator first, (until const_iterator last, C++20) size_type count2, CharT ch ); (3) constexpr basic_string& replace( const_iterator (since first, const_iterator last, C++20) size_type count2, CharT ch ); basic_string& replace( (since const_iterator first, C++11) const_iterator last, (4) (until std::initializer_list<CharT> C++20) ilist ); constexpr basic_string& replace( const_iterator (since first, const_iterator last, (4) C++20) std::initializer_list<CharT> ilist ); template < class StringViewLike >
(5) (since basic_string& replace( C++17) size_type pos, size_type (until count, C++20)
(5) const StringViewLike& t ); template < class StringViewLike >
(6) constexpr basic_string& (since replace( size_type pos, C++20) size_type count,

const StringViewLike& t ); (6) template < class StringViewLike >
(since basic_string& replace( C++17) const_iterator first, (7) (until const_iterator last, C++20)

const StringViewLike& t ); template < class StringViewLike > (8)

constexpr basic_string& (since replace( const_iterator C++20) first, const_iterator last,

const StringViewLike& t ); template < class StringViewLike > (8)

basic_string& replace( (since size_type pos, size_type C++17) count, const StringViewLike& (until t, C++20)

size_type pos2, size_type count2 = npos ); (9) template < class StringViewLike >

constexpr basic_string& replace( size_type pos, (since size_type count, const C++20) 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).


This overload has the same effect as overload (6) if InputIt is an (until C++11)
integral type.
This overload only participates in overload resolution if InputIt (since C++11)
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;. 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. 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 - 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)

Example

// Run this code


#include <cassert>
#include <cstddef>
#include <iostream>
#include <string>
#include <string_view>


std::size_t replace_all(std::string& inout, std::string_view what, std::string_view with);
std::size_t remove_all(std::string& inout, std::string_view what);
void test_replace_remove_all();


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\n";


test_replace_remove_all();
}


std::size_t replace_all(std::string& inout, std::string_view what, std::string_view with)
{
std::size_t count{};
for (std::string::size_type pos{};
inout.npos != (pos = inout.find(what.data(), pos, what.length()));
pos += with.length(), ++count) {
inout.replace(pos, what.length(), with.data(), with.length());
}
return count;
}


std::size_t remove_all(std::string& inout, std::string_view what) {
return replace_all(inout, what, "");
}


void test_replace_remove_all()
{
std::string str2{"ftp: ftpftp: ftp:"};
std::cout << "#1 " << str2 << '\n';


auto count = replace_all(str2, "ftp", "http");
assert(count == 4);
std::cout << "#2 " << str2 << '\n';


count = replace_all(str2, "ftp", "http");
assert(count == 0);
std::cout << "#3 " << str2 << '\n';


count = remove_all(str2, "http");
assert(count == 4);
std::cout << "#4 " << str2 << '\n';
}

Output:


A quick red fox jumps over the lazy dog.


#1 ftp: ftpftp: ftp:
#2 http: httphttp: http:
#3 http: httphttp: http:
#4 : : :


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 1323 C++98 several overloads of replace use iterator use const_iterator
and not const_iterator
LWG 2946 C++17 string_view overload causes ambiguity in avoided by making it a
some cases template

See also


regex_replace replaces occurrences of a regular expression with formatted
(C++11) replacement text
(function template)
replace replaces all values satisfying specific criteria with another value
replace_if (function template)

2022.07.31 http://cppreference.com