Friday, October 24, 2008

Flash AS3 String Prototype Extension

Problem: 
I was working on a problem where two strings looked identical but weren't.

Solution:
Because there were many variations of the string, it was possible that some converting (and integration with stage objects:textfields) may have changed the white space characters to something different. I was able to get character codes for what existed and what I wanted to replace it with, so I wrote a simple prototype extension for the String class class replaceCodes(code1, code2), and it would change the string to reflect the replacement.

The solution/function:
String.prototype.replaceCodes = function(code:uint, code2:uint):String {
    return    this.split(String.fromCharCode(code)).join(String.fromCharCode(code2));
}

Hope this helps someone.

No comments: