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!!
August 5th, 2010 on 5:10 am
thanks
October 14th, 2010 on 4:27 pm
Very use example, thanks a lot!
December 29th, 2010 on 9:18 am
thanks,
but
what about when the string begins with the pattern
December 29th, 2010 on 9:22 am
example:
str = “hello world hello everyBody”;
StringReplaceAll( str,”hello”, “good morning” );
result:
str = “world good morning everyBody”
March 21st, 2011 on 2:15 pm
hey all,
the replace all is needed in the sense that replace only replaces Once. so if you have multiple occurrences of the string you are trying to replace, it will not work.
Thanks for tip colin.
April 22nd, 2011 on 2:01 pm
My, what a fun thread this is to read…
Flex (ActionScript, actually) lets you use Regular Expressions for your search string.
Regular Expressions already have the /g modifier, which already means replace all. If you use this modifier in your expression, the replace function will replace all.
Regular Expressions are becoming the deFacto way to provide a powerful variety of search and replace algorithms.
Why should language architects reproduce all the functionality of RegEx with their own custom code?
For example, one could use the replace function as-is to replace any email in a string with the word [redacted]. That’s how cool RegEx is. Or should they also provide a replaceEmailWith() function as well?
There are a wealth of resources out there to help one learn how to write RegEx. It’s important to learn, not just for ActionScript, but to keep pace with the evolution of code in general.
All that said… your function IS a neat way to do the same thing.
May 5th, 2011 on 8:18 am
thanks!
June 7th, 2011 on 8:28 am
if you can even use a regexp you should not call yourselfs developers… !!
November 15th, 2011 on 7:38 pm
I wouldn’t want to work with Chris.
November 21st, 2011 on 10:59 pm
I wouldn’t want to work with Josh.
Seriously though… I come from a decade of .NET and using String.Replace() to replace ALL occurrences of the string is very helpful. I got stuck in the side when I assumed AS3 behaved the same.
Oh well, I can adjust. The rest of AS3 is A-OK to me.
December 1st, 2011 on 6:03 am
Thanks..Really nice..
December 12th, 2011 on 5:02 pm
I have to admit, given the number of languages which do provide this implementation, I was surprised at first that a replaceAll() method was not provided. However, I use regex frequently, so I simply implemented an old BSD regex pattern I used in Vi. One could argue that most interfaces in an API are wrapping redundant functionality, and therefore those functions/methods are not absolutely necessary. But the primary purpose of an API is to expedite the construction process. And to force someone to put on the regex hat to modify a string seems a bit confounding when a method could be provided which is self-documenting, therefore intuitive, therefore easy to implement. That and not all regex engines were created equally, though for this requirement they nearly all work the same I think. The word developer doesn’t refer to someone who knows all there is to know, if that were true, there would be no such creature. Anyone who is reading this is a developer, they knew enough to research, and found a solution, whether they accept it or not.
December 12th, 2011 on 5:21 pm
Also, if it’s really that important, you could simply extend the String class and add a replaceAll method, but you would likely find that you will be wrapping the existing replace method which will imbed the replace string in a regular expression. This is interesting because at least in Java the String class can not be extended, and for good reason, it’s immutable and thereby was declared final predominantly due to concepts such as serializing of object data. A remote VM must have a consistent contract of what a String object is and it’s behaviors. However, Flex is probably not going to span over an enterprise solution, so this is moot and the String implementation can be extended. But to what purpose?
January 22nd, 2012 on 2:25 am
VERY VERY THANK YOU SO MUCH!
Muito Obrigado mesmo!
February 2nd, 2012 on 12:00 pm
Thanks for some other informative blog. The place else could I get that kind of information written in such a perfect manner? I have a undertaking that I am just now working on, and I have been at the glance out for such information.