Customizing toString()
By sascha, September 8, 2008 – 2:15 am -When writing classes for a framework I usually put a custom toString method into important classes so that debugging becomes easier. Normally they would go like something along the lines of:
override public function toString():String { return "[ClassName]"; }
… Sometimes adding properties to the returned String that give back information about the class, e.g.
override public function toString():String { return "[ImageClass, size=" + _size + "]"; }
But writing them rigid like that is a disadvantage when you decide later to refactor class names. Admittedly it’s also not a very elegant way so I got the idea to take the class name that is returned by getQualifiedClassName(). The only problem is that getQualifiedClassName not only provides the type name but also the whole package String of the class. Regular Expressions to the rescue! After twiddling around with them for a while (I’m by no means a RegExp expert) I got my toString method into the shape that I desired:
override public function toString():String { return "[" + getQualifiedClassName(this).match("[^:]*$")[0] + ", size=" + _size + "]"; }
This way it matches the String returned by getQualifiedClassName with the Regular Expression [^:]*$ which checks from the right end for an arbitrary text up to the first occurring colon, but without including the colon. Taking the first element of the Array returned by match() and you got what you need!
A nice way of using this is when writing abstract classes that contain the toString method and any subclass can use that toString method without the need to override it … that is of course unless you want ot add other output information.
Featured
Using Kore 2 more efficiently with User Tags

Kore 2 from Native Instruments is probably every sound organizer’s wet dream with sugar on top. Not only can it control a multitude of Softsynths but it’s database makes it easy to organize sounds and find them quickly when needed. I’ve purchased this nice tool last December and what’s better than telling a few details on how I put it’s features to good use?!
Continue Reading »Memories of Star Wars Galaxies
March 20, 2006 – 3:59 pmI've been playing Star Wars Galaxies on and off for about three years so by the time a large amount of screenshots have been piled up on my harddisk. And ...
Tomb Raider Legends First 10 Minutes Video
March 20, 2006 – 5:05 amPlaysyde has published a video of the first 10 minutes playing into Tomb Raider Legends. This is the first time you can really see how the game looks in action ...
Sound Object for future Flash Player
March 19, 2006 – 10:37 pmIf you're one of the users who would like to see something done about Flash's neglected sound situation in future then I recommend you to visit the Adobe Labs forum ...
Star Wars Galaxies - A slight breath of hope?
March 10, 2006 – 5:43 am[caption id="attachment_244" align="alignright" width="197" caption="What do you need spells for if you got a decent Blaster Rifle?!"][/caption] According to this forum post on allakhazam.com, the result of Lucas Arts Focus Group ...
Flash Game Programming Wiki opened
January 9, 2006 – 10:36 pmI am happy to announce the opening of the Flash Game Programming Wiki, a Wiki that is dedicated to the development and programming of games with Flash! The idea for ...
The rise and fall of a Galaxy
November 18, 2005 – 7:27 amOr how to completely destroy a popular MMORPG [ad#ad_content] I've been a Star Wars fan for a long time, I remember since 1983 when I still was a kid and went ...
Flash Racer Blog
August 23, 2005 – 10:35 pmA development blog about a racing game done in Flash! Some interesting posts there, one about AI for computer controlled racing cars. It seems they will update their blog as ...
Review: iPod Shuffle
August 16, 2005 – 12:25 pmI've bought myself an iPod Shuffle finally after pondering about a couple of mobile players. This device is the second best thing after sliced bread. I don't wanna dig to ...
Biiig biiig ad!
August 5, 2005 – 9:51 amThe coolest commercial I've seen since a long time .... http://www.bigad.com.au/! No, it won't hurt your system, despite the security warning that shows up eventually, but this is so ...
Tools Of The Trade Part 1: Alias SketchBook Pro
July 25, 2005 – 10:32 pmAs a one-man-game developer of worldclass (exaggeration intended!) it's good to have a bunch of tools at hand that help with the tasks for creating games! In this series I ...
| Filed under »