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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change a space to " & "?

Status
Not open for further replies.

Placido

MIS
Mar 13, 2002
105
IT
I'm trying to change a space to this string " & " without quotes naturally...
 
Placido,

this will work, as long as the string to remove (sOld) is only one character long. sNew can be any length.

Code:
function doReplace(sText, sOld, sNew) {
	//  sText:  the string of text
	//  sOld:  the character to replace with sNew
	var sTemp = "";

	for (idx = 0; idx < sText.length; idx++) {
		if (sText.charAt(idx) == sOld)
			sTemp += sNew;
		else
			sTemp += sText.charAt(idx);
	}
	return sTemp;
}

you would call it in this manner:

doReplace(&quot;i am a string of text&quot;,&quot; &quot;,&quot; & &quot;);

you can also pass a variable or the contents of a form field as sText, sOld or sNew.
======================================

if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top