Flex doesn’t have a String#replaceAll()?
by Colin on Jul.15, 2008, under Flex, RIA
What?!? I was shocked to learn that the latest Flex framework / Actionscript doesn’t have an equivalent for a replaceAll() on String. I’m looking for a simple way to do replacements on a string. Most of the languages that I’ve worked with have such a method or a library to provide that functionality. PHP has the str_replace function, Java has a replaceAll() on java.lang.String, Python has it, C++ has libraries that readily provide this functionality, the System.String in the .NET framework(1.0,1.1,2.0, etc.) has a string#replace method, even in the RIA space, Silverlight has a replace method on System.String, as does JavaFX (java.lang.String).
After searching and reading for a while, the closest equivalent that I could find is a custom method that utilizes the split and join functionality like the following:
public static function StringReplaceAll( source:String, find:String, replacement:String ) : String
{
return source.split( find ).join( replacement );
}
The preceding function came from Base64.as from Jason Nussbaum’s blog post about Base64 encoding/decoding. Others have used similar functionality like this post on flexfanatic. Its definitely better than while loop.
I also found that it is possible to utilize a RegExp within the String#replace function as shown on SCRIBBLE IT. Basically the code would look like:
var str:string = "Somesilly String. silly!";
str.replace(new RegEx("silly", "g") " awesome");
With this pattern it can still be a one-liner, which should preform better than the split/join methodology, but I am still shocked that such a standard method isn’t in the framework. I am a bit surprised by this finding. is there a better way? A good StringUtil Library or something similar?
December 22nd, 2008 on 9:54 am
Thank you very much
June 10th, 2009 on 2:21 pm
And why don’t you just use the String.replace method correctly?
str:String = “test xxx “test” yyy”;
str.replace(/test/g, “WOW”);
// str is now “WOW xxx “WOW” yyy”
Before you are shocked the next time and blame the framework learn how to use it correctly.
People here will read your post and then think bad about the flex framework. But the truth is, that is has a very powerfull replace method.
June 10th, 2009 on 9:27 pm
@Chris,
Its plain and obvious in the docs (as of Flex 3.3/), I must have been looking at the 3.0 documents last June and July(http://en.wikipedia.org/wiki/Adobe_Flex#Release_history).
It is definitely a one-liner to use a global regex, but its not intuitively obvious especially coming from other frameworks
http://livedocs.adobe.com/flex/3/langref/String.html#replace()
I still would contend that on a class as crucial as String, it should have a replaceAll(x,y) using str.replace(/test/g, “WOW”); can get the Job done, but I think we can still do better.
December 7th, 2009 on 2:10 pm
How do I do a global replace when the regexp is unknown until runtime? ie
s = s.replace(“\$\{” + name + “\}”,value)
January 6th, 2010 on 5:41 am
thanks man. helped me!
February 25th, 2010 on 3:00 pm
Flex sucks!!!
March 15th, 2010 on 4:04 am
I think the point the guy was making is that it would be nice if flex provided an alternative method to regular expressions which not everybody enjoys using, as most other languages do. You don’t have to jump down his throat because you disagree with that point of view.
March 28th, 2010 on 9:19 am
All I want is to replace all occurences of the letter “A” in a string. And I just can’t figure it out.
Regex makes my head spin and I don’t even know where to begin bashing that as a solution. It just doesn’t work, no clue why.
Colin is right to find it absurd that there is no standard “replace all” function.
April 23rd, 2010 on 8:30 am
Wonderful
June 12th, 2010 on 1:19 am
Really nice!!