Colin Harrington

Flex doesn’t have a String#replaceAll()?

by on Jul.15, 2008, under Flex, RIA

Flex Logo on WhiteWhat?!?  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?

:

25 Comments for this entry

  • sten

    Thank you very much

  • chris

    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.

  • Colin

    @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.

  • Lewis

    How do I do a global replace when the regexp is unknown until runtime? ie

    s = s.replace(“\$\{” + name + “\}”,value)

  • Lala

    thanks man. helped me!

  • Kelvin

    Flex sucks!!!

  • Garry

    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.

  • Andrew

    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.

  • Kishore

    Wonderful

  • Aruna

    Really nice!!

  • vmaksym

    Very use example, thanks a lot!

  • zahroo

    thanks,
    but
    what about when the string begins with the pattern

  • zahroo

    example:

    str = “hello world hello everyBody”;

    StringReplaceAll( str,”hello”, “good morning” );

    result:

    str = “world good morning everyBody”

  • perfi

    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.

  • Al

    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.

  • Thanker

    thanks!

  • Josh

    if you can even use a regexp you should not call yourselfs developers… !!

  • montana

    I wouldn’t want to work with Chris.

  • Texas

    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.

  • Micheal

    Thanks..Really nice..

  • Davis

    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.

  • Davis

    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?

  • Natan Cabral

    VERY VERY THANK YOU SO MUCH!
    Muito Obrigado mesmo!

  • diet way

    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.

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!