Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

15
  • 3
    How would I fix it if the original string had more that one instance of "$name" and I wanted to replace all of them. Commented Aug 5, 2010 at 19:14
  • 3
    Why aren't from and to passed per const reference? What does your function if from isn't there? -1 from me for that. Commented Aug 5, 2010 at 19:15
  • 15
    @sbi Fixed, although you could've phrased it as recommendations instead of attacks -- it simply didn't occur to me, I rarely think to use const and if I wrote a utility method like this I would only call it if I knew the replacement were valid Commented Aug 5, 2010 at 19:19
  • 14
    @Michael: Good, I turned my down-vote into an up-vote. Dismissing const is disregarding one of C++' best tools. Passing per const reference should be the default mode for function parameters. (FTR, without the const, you couldn't even pass string literals to your function, because you cannot bind temporaries to non-const references. So the function wouldn't even do what it was written to.) Commented Aug 5, 2010 at 21:13
  • 86
    Is this still the only solution in 2018? If so and any C++ committee are reading this, sort it out. It's embarrassing. split(string, string) and replace(string, string) please! Commented Jul 31, 2018 at 12:53