Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


String.replace

Flash Version: 5
Author: info@sephiroth.it
Total downloads: 18505
Category: String
Publish Date: 2002-02-20 01:53:57

the code:
// ----------------------- 
// Replace single or 
// multiple chars in a 
// String. 
// The original string is 
// not affected. 
// 
// Based on a idea of 
// Davide Beltrame (Broly) 
// mail: davb86@libero.it 
// ----------------------- 
String.prototype.replace = function() 
{
var arg_search, arg_replace, position; 
var endText, preText, newText; 
arg_search = arguments[0];
arg_replace = arguments[1];
if(arg_search.length==1) return this.split(arg_search).join(arg_replace);
position = this.indexOf(arg_search); 
if(position == -1) return this; 
endText = this; 
do 
{ 
position = endText.indexOf(arg_search); 
preText = endText.substring(0, position) 
endText = endText.substring(position + arg_search.length) 
newText += preText + arg_replace; 
} while(endText.indexOf(arg_search) != -1) 
newText += endText; 
return newText; 

} 

// --------------------- 
// USAGE: 
// --------------------- 
originalString = "This is the original fucking text. What the hell are you typing?" 
replacedText = originalString.replace('fuck','***'); 
trace("-----------------------------"); 
trace("original was: " + originalString); 
trace("replaced is: " + replacedText);

Comments (11)

raima156 (from Lithuania ) on 22nd August 2006

oh...

// ---------------------
// USAGE:
// ---------------------
originalString = "This is the original fucking text. What the hell are you typing?"
replacedText = originalString.replace('fuck','***');
trace("-----------------------------");
trace("original was: " + originalString);
trace("replaced is: " + replacedText);

shorter.. and works allmoust 2 times faster.

raima156 (from Lithuania ) on 22nd August 2006

// -----------------------
// Replace string in a string with a string; ;)
// 2006.08
// Raimundas Banevicius (Deril)
// mail: raima156@yahoo.com
// -----------------------
String.prototype.replace = function() {
var arg_search:String = arguments[0], arg_replace:String = arguments[1];
var preText:String = this, newText:String = "";
var tempArr:Array = preText.split(arg_search);
newText = tempArr[0];
for (var i = 1; i<tempArr.length; i++) {
newText += arg_replace+tempArr[i];
}
return newText;
};

kanukukreja (from India ) on 27th June 2006

originalStr="kanu is a good girl"

replaceStr="Anu" with "*******"

returnedStr="kanu is a good girl"

but i want

returnedStr="k****** is a good girl"

tell me d way of doing dis

faheem (from Pakistan ) on 16th March 2006

what if I want to search and replace non case senctive ?

like if my string is

a = "Hello world hello";

and I want result as

a = "HLO world HLO";

any sugestions ?

idont on 31st May 2005

here's my solution which fits best for my needs:

var my_string = "This is my solution.";
var searchString = "my";
var replaceString = "your";
my_array = my_string.split(searchString);
if (my_array.length > 0) {
my_string = my_array.join(replaceString);
}
trace(my_string);

bogus on 29th May 2005

actually, if it appears more than once in your code, you'd have to add:

if (newText == undefined) {
newText = "";
}

right before:

position = endText.indexOf(arg_search);

But if it only appears once in the search text, jchavez's version would work.

jjones on 8th December 2004

frederik@d-e-sign.be is the bomb, the shortest code and possibly the best working find actionscript string replace function. It worked great the other examples I found were long and complicated can't believe it's that easy.

varuna123 on 22nd November 2004

Can someone help me out with an Actionscript 2.0 translation of this code?

TIA!

jchavez on 12th August 2004

Add this line:

newText ="";

right before:

position = endText.indexOf(arg_search);

So it will work fine on Flash MX 2004. Without it it will add "Undefined" for every ocurrence.

pietro on 25th March 2003

great solution man ;)