Author: info@sephiroth.it
Total downloads: 18505
Category: String
Publish Date: 2002-02-20 01:53:57
// -----------------------
// 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)
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.
// -----------------------
// 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;
};
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
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 ?
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);
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.
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.
Can someone help me out with an Actionscript 2.0 translation of this code?
TIA!
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.
great solution man ;)
