Split & Join: What you need to know about replace operation on Strings!

Friday, August 31, 2012

Once upon a time long long ago, in ActionScript 2, String.replace method was missing (something that can be seen in most other major programming languages).

After many years, 'replace' method came along in String class of ActionScript 3. But, not many folks were happy. Because, in ActionScript 3, string.replace(arg1, arg2), replaces only the first occurrence of the arg1 with arg2.

In order to perform a ‘replace all’ operation use

strTarget.replace(/arg1/g, arg2);
The 'g' is just a handle to specify that all occurrences of the string arg1 need to be replaced

or
 
strTarget.split(arg1).join(arg2);

Point to note here is that, in the both the methods, traditional 'replace' & the 'split-join' method arg1 can be of type string or a regular expression as the need be.

No comments:

Post a Comment

 

About Me

My photo
Front-end Developer with over 9 years of experience in developing Web 2.0 sites with a keen eye for UX. Avid technology enthusiast; gadget freak; old school PC gamer; loves developing Apps & Data Visualizations.

My Website

Stack Overflow profile

Twitter Profile

Facebook profile