absl::StrReplaceAll

Replaces character sequences within a given string with replacements provided within an initializer list of key/value pairs.

Synopsis

Declared in <absl/strings/str_replace.h>

[[nodiscard]]
std::string
StrReplaceAll(
    std::string_view s,
    std::initializer_list<std::pair<std::string_view, std::string_view>> replacements);

Description

Candidate replacements are considered in order as they occur within the string, with earlier matches taking precedence, and longer matches taking precedence for candidates starting at the same position in the string. Once a substitution is made, the replaced text is not considered for any further substitutions.

Example:

std::string s = absl::StrReplaceAll( "\(who bought \)count #Noun. Thanks $who!", {{"$count", absl::StrCat(5)}, {"$who", "Bob"}, {"#Noun", "Apples"}}); EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);

Return Value

A new string with the replacements applied.

NOTE

The return value should not be discarded.

Parameters

NameDescription
sThe string to search for replacements.
replacementsThe key/value pairs of sequences to replace.