Posted by Stooshie (Stooshie), 23 September 2002Not sure if this is the right forum for this.
which is fine, but I wanted a function to search and replace one string with another string in the main string (which you can probably do with the above function, but I wanted the challenge )
I started thinking about looping round the string, checking for a match to the replace string, deleting the number of characters = length of replace string, inserting the with string and moving on the length of the with string etc...
Then I found this Genius piece of code (the genius is in it's simplicity, I think).
Maybe the experts on this board have seen it before, but hey, I was amazed.
Hope somebody finds it useful.
Posted by admin (Graham Ellis), 23 September 2002Hi ... welcome ...
Stooshie, your Java script is elegant. At first, no doubt it seems an extaordinary way of replacing strings by splitting and joining in this way - but if you look at a language like PHP, you'll find that the recommended way of doing something like this is
a) explode the string at the string you want to be replaced
b) implode the string, joining with the replacement string.
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho