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.

6
  • 9
    While this is probably a suitable solution when the number of characters to replace is small compared to the length of the string, it doesn't scale well. As the proportion of characters in the original string that need to be replaced increases, this method will approach O(N^2) in time. Commented May 24, 2010 at 14:37
  • 7
    True. My general philosophy is to do the easy (to write and to read) thing until such time as the inefficiencies are causing real problems. There are some circumstances where you might have humoungous strings where O(N**2) matters, but 99% of the time my strings are 1K or less. Commented May 25, 2010 at 3:40
  • 4
    ...that being said, I like Kirill's method better (and had already voted it up). Commented May 25, 2010 at 3:41
  • What happens if "x" is not found? Also, why you are using double braces? Commented Aug 25, 2015 at 11:26
  • @PrasathGovind - I was just showing the calls required (hence "something like"). Important but obscuring details such as proper error handling were left as an exercise for the reader. As for "double braces", I'm not sure what those are, or what you are talking about. For me a "brace" is the { character. I don't know what a "double brace" is. Perhaps you have some kind of font issue? Commented Aug 25, 2015 at 12:54