Domino Code Fragment

Code Name*
JavaScript function for doing search and replace of a character within a string
Date*
07/08/2000
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.
Description*
The following function takes three parameters -- the string upon which it operates, the character it is searching for, and the character it is replacing that character with. It returns the new string product of this search and replace operation, and works in Navigator 3x and 4x, as well as IE 3x and 4x. This can be useful if, say, you are doing parameter passing to a Query_String field in a domino document via the URL, and need to replace the spaces in the text you are passing in the URL with underscore characters. If you are confined to using JavaScript to accomplish this search and replace operation, then this function will do the trick.
Type*
JavaScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:

function searchAndReplace(theString,oldChar,newChar) {
var browser = (navigator.appName == "Microsoft Internet Explorer")?"Explorer":"Netscape";
var gen = parseInt(navigator.appVersion);
if ((browser == "Netscape" && gen >= 3) || (gen >= 4)) {
var words = theString.split(oldChar);
var newString = words.join(newChar);
return newString
}
else {
var words = new Object();
var placeHolder = theString.indexOf(oldChar);
var counter = 0;
words[counter] = theString.substring(0,placeHolder);
var remainder = theString.substring(placeHolder + 1, theString.length);
while (remainder.indexOf(oldChar) >= 1){
counter = counter + 1;
placeHolder = remainder.indexOf(oldChar);
words[counter] = remainder.substring(0,placeHolder);
remainder = remainder.substring(placeHolder + 1,remainder.length);
}
counter = counter + 1;
words[counter] = remainder;
var newString = "%";
for (prop in words){
newString = newString + newChar + words[prop];
}
newString = newString.substring(2,newString.length);
return newString

}
};