Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing characters problem 2

Status
Not open for further replies.

pixelz

Programmer
May 8, 2006
32
GB
Hi there,

I've written a small app to get a news headline and story URL out of my MSSQL database and convert it into XML and into an RSS feed. Problem is, of course, that RSS doesn't like '?' or '&' of which my URLs have plenty. If it's an internal url, then no problem, but the minute I use something like: "it doesn't like it!

Now I've written a little function to help me to replace those characters using the replace() function in JScript. This however, just hangs the page until I get a script timout error! Hmmmm.... I've isolated and isolated and it really seems to be the replace function that is at issue. Here's my code:

//I define a new object to hold my conversion values:
function charObject (oldChar, newChar) {
this.oldChar = oldChar;
this.newChar = newChar;
}

//Here is the array of those objects - what to change and what to change to:
var charArray = new Array;
charArray[charArray.length] = new charObject('?','?');
charArray[charArray.length] = new charObject('&','&');

//And this is the replace function:
function replaceChar(thisString) {
//set some variables to ensure correct datatyping
var strReplaceAll = new String(thisString);
var arrLength = charArray.length;

//Now loop through the object array
for (i=0; i<arrLength; i++) {

//check for match
var indexOfMatch = strReplaceAll.indexOf(charArray.oldChar);

//if match, loop through the string while that match exists replacing as we go - the problem seems to be with this loop! even if I just put a Response.Write in there, it still hangs.
while (indexOfMatch != -1) {
//replace first char instance
strReplaceAll = strReplaceAll.replace(charArray.oldChar, charArray.newChar);
//find occurance of next char instance, if it exists
indexOfMatch = strReplaceAll.indexOf(charArray.oldChar);
}
}
//return completed string
return strReplaceAll;
}

For your info, I call this function by doing this:

var urlString = new String(objRec('includeURL'));
var newUrlString = replaceChar(urlString);

where includeURL comes out of my database record (looks like URL above)

Anybody have any idea why this script times out? and do you have any suggestions for how I can work around it?

Thanks SO much!

Pix
 
Will do!

and will do the same for your post too!

;)
 
If you put in <![CDATA[...]]> to wrap around the text of "link" and "guid" nodes, I don't see why you keep &amp; there for (meaning re-dress it as "&"). &#63; being the character entity of "?", you can keep it. But all that would be very tiring to explain. I'll just leave you this note.
 
Yeah, tsuji, I should have replaced all the char codes as literals, I did notice this after I posted earlier, but since it works either way, I didn't bother.

And, glad I could help pixelz.

[monkey][snake] <.
 
Yup - actually when I put the CDATA tags in, the urls that I had hardcoded with the replacement unicode stopped working completely, so they definately needed replacing!

Thank for pointing that out, tsuji!

;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top