Asked  7 Months ago    Answers:  5   Viewed   64 times

What is the effective way to replace all occurrences of a character with another character in std::string?

 Answers

79

std::string doesn't contain such function but you could use stand-alone replace function from algorithm header.

#include <algorithm>
#include <string>

void some_func() {
  std::string s = "example string";
  std::replace( s.begin(), s.end(), 'x', 'y'); // replace all 'x' to 'y'
}
Tuesday, June 1, 2021
 
tdous
answered 7 Months ago
18

Update: In the latest versions of most popular browsers, you can use replaceAll as shown here:

let result = "1 abc 2 abc 3".replaceAll("abc", "xyz");
// `result` is "1 xyz 2 xyz 3"

But check Can I use or another compatibility table first to make sure the browsers you're targeting have added support for it first.


For Node and compatibility with older/non-current browsers:

Note: Don't use the following solution in performance critical code.

As an alternative to regular expressions for a simple literal string, you could use

str = "Test abc test test abc test...".split("abc").join("");

The general pattern is

str.split(search).join(replacement)

This used to be faster in some cases than using replaceAll and a regular expression, but that doesn't seem to be the case anymore in modern browsers.

Benchmark: https://web.archive.org/web/20170308183046/http://jsperf.com/replace-all-vs-split-join

Conclusion: If you have a performance critical use case (e.g processing hundreds of strings), use the Regexp method. But for most typical use cases, this is well worth not having to worry about special characters.

Tuesday, June 1, 2021
 
EurekA
answered 7 Months ago
39
string [] items = {"one","two","three","one","two","one"};
items =  items.Select(s => s!= "one" ? s : "zero").ToArray();

Found answer from here.

Saturday, July 31, 2021
 
aaronhuisinga
answered 5 Months ago
56

The first one should be "\\", not "\". It works like this:

  • You have written "\".
  • This translates to the sequence in a string.
  • The regex engine then reads this, which translates as backslash which isn't escaping anything, so it throws an error.

With regex, it's much easier to use a "verbatim string". In this case the verbatim string would be @"\". When using verbatim strings you only have to consider escaping for the regex engine, as backslashes are treated literally. The second string will also be @"\", as it will not be interpreted by the regex engine.

Thursday, August 5, 2021
 
Trott
answered 4 Months ago
33

You should walk the DOM, find text nodes, and replace the found text in each.

Here's a simple example. You can make walkText() more generic by passing a callback that does the replacement.

function walkText(node) {
  if (node.nodeType == 3) {
    node.data = node.data.replace(/foo/g, "bar");
  }
  if (node.nodeType == 1 && node.nodeName != "SCRIPT") {
    for (var i = 0; i < node.childNodes.length; i++) {
      walkText(node.childNodes[i]);
    }
  }
}
walkText(document.body);
foo <b>foo</b> foo
Monday, November 22, 2021
 
user3284707
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share