String.Format()

This is another one of those utilities that my work requires… well actually it doesn’t I just do this for convenience…

used when concatenating strings with values… I’m going to use a url as an example.

Say we have a variable for our url, and the text to display…

ActionScript Code:
[FONT=Courier New][LEFT][COLOR=#000000]**var**[/COLOR] [COLOR=#0000FF]url[/COLOR] = [COLOR=#FF0000]"http://www.kirupa.com"[/COLOR];

[COLOR=#000000]var[/COLOR] label = [COLOR=#FF0000]“kirupa’s Great Website”[/COLOR];
[/LEFT]
[/FONT]

the way you’d bulid a link out of this would look something like…

 ActionScript Code:
 [FONT=Courier New][LEFT][COLOR=#000000]**var**[/COLOR] linkHtml = [COLOR=#ff0000]"<a href='"[/COLOR] + [COLOR=#0000ff]url[/COLOR] + [COLOR=#ff0000]"'>"[/COLOR] + label + [COLOR=#ff0000]"</a>"[/COLOR];

[/LEFT]
[/FONT]

Now this will work fine, but it’s ugly, and can get to be not fun when working with complicated insertions. Luckily as a C# developer I have the luxury of using something called the String.Format() method which allows us to do this…

build our string…

 ActionScript Code:
 [FONT=Courier New][LEFT][COLOR=#000000]**var**[/COLOR] [COLOR=#0000ff]url[/COLOR] = [COLOR=#ff0000]"http://www.kirupa.com";

var label = “[/COLOR]Kirupa[COLOR=#ff0000]'s Great Website”;
var linkHtml = “<a href=’[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000080]0[/COLOR][COLOR=#000000]}[/COLOR][COLOR=#ff0000]’>{1}</a>”;

var final_html = stringFormatter(linkHtml, url, label); [/COLOR]
[/LEFT]
[/FONT]

The syntax is String.Format(string, args);

you’ll notice in our string, we have {0} and {1}… these are our replacement markers… and our args are the arguments to replace… in our example…

url is argument0 and label is argument1, so anywhere we see {0} it will be replaced with argument0 (url) and anywhere we see {1} it will be replaced with argument1 (label)… we can do this with as many arguments as we’d like.

I hope that some of this came in clear, here’s the class


 // this is a class we will use to transform strings
// it will grow as we see fit.
// for the most part just a method library geared towards
// strings specifically

/*
 *
 *@author Michael Avila
 *@version 1.3
 *@description This is a class for handling strings.
 */
class StringUtility
{
    
    /* Replaces all occurances of one series of characters with another series of characters
     *
     * @param The string you are invoking the replace on.
     * ...
     * @param The series of characters which you plan on replacing
     * ...
     * @param The series of characters you are replacing with.
     * 
     * @return The string with our values replaced
     * 
     * @usage <pre>StringUtility.replace("Hello World", "World", "Michael");</pre>
     */
     public static function replace(original : String, string1 : String, string2 : String) : String
     {
         var return_string : String = "";
         
         var start_position : Number = 0;
         var end_position : Number = 0;
        
         while (true)
         {
            start_position = original.indexOf(string1, end_position);
            
            if (start_position == -1) break;
            
            return_string += original.substring(end_position, start_position);
            return_string += string2;
            
            end_position = start_position + string1.length;
         }
         
         return_string += original.substring(end_position, original.length);
         
         return return_string;
     }
    
    /* Returns whether or not your series of characters is contained within this string
     *    @param The string you are going to be looking in
     *  ...
     *  @param The strings of characters that you will be looking for.
     * 
     *  @return Returns true if all the specified arguments are contained in the string
     *             and false if not.
     * 
     *  @usage <pre>StringUtility.contains("this is the search string", "this", "search");</pre>
     */
    // checks whether or not a string contains a specific series of characters

    public static function contains(the_string : String, args : String) : Boolean
    {
        // how many different strings are we checking for...
        var strings : Number = arguments.length-1;
        
        // loop through how many we are checking for, and 
        for (var i:Number=0; i<strings; i++)
        {
            if (the_string.indexOf(arguments[i+1]) == -1)
            {
                // if the string does not contain what we asked for.
                return false;
            }
        }
        
        // if we get this far, then the string did contain everything we asked for...
        return true;
    }
    
    /* Inserts values into a string.
     *  @param The string you are going to be inserting values into
     *  ...
     *  @param The values that you will be adding into the string
     * 
     *  @return Returns the string with all of your values inserted
     * 
     *  @usage <pre>StringUtility.insertion("Hello: {0}, this is visit number {1}.", "Michael", "20");</pre>
     * 
     */
    public static function insertion (original : String, args : String ) : String
    {
        // some constants.. what are markers tags are...
        var MARKER_OPEN : String = "{";
        var MARKER_CLOSE : String = "}";
        // The string with all the values set...
        var new_string : String = "";
        // represents our current search position of the character {
        var position_1 : Number = null;
        // represents our current search position of the character }
        var position_2 : Number = null;
        // the number that represents the marker (argument) value that we are currently on
        var argument : Number = 0;
        // the string we are replacing our argument with
        var argumentValue : String = "";
        while (true)
        {
            // the position we are searching from...
            var search_from : Number;
            // if the position has not been set, start from the beginning
            if (position_1 == null)
            {
                search_from = 0;
            } else
            {
                search_from = position_1 + 1;
            }
            // set position to the next index of the next instance of our MARKER_OPEN
            position_1 = original.indexOf (MARKER_OPEN, search_from);
            // if there are no more instances of MARKER_OPEN in this string, then break from the loop
            if (position_1 == - 1) break;
            
            // where we will begin the slicing of our string at... start at 0
            var start : Number = 0;
            // where we will end the slicing of our string at... end at position_1
            var end : Number = position_1;
            // if position_2 has been set, then start is always the character index AFTER position_2
            if (position_2 != null)
            {
                start = position_2 + 1;
            }
            // add to new_string the contents between this and the last marker
            // if this is the first marker then it just adds from the beginning of the string
            new_string += original.substring (start, end);
            
            // set position_2 to the index of MARKER_CLOSE after our current MARKER_OPEN
            position_2 = original.indexOf (MARKER_CLOSE, position_1);
            // parse the argument as an integer.. requires string manipulation to pull the marker
            // value out.
            argument = parseInt (original.substring (position_1 + 1, position_2));
            // pull from the arguments array the value that will replace this marker...
            argumentValue = arguments [argument + 1];
            // add our argument value, so that our new_string no longer contains a marker, but
            // instead the value
            new_string += argumentValue;
            
        }
        // when we are done, there are no more markers, so just add on the remainder 
        // of our original string from the last marker on, onto our new_string
        new_string += original.substring (original.lastIndexOf (MARKER_CLOSE) + 1, original.length);
        // return our string with all the inserted values
        return new_string;
    }
}


******* UPDATED
Now has contains and replace methods… and a documentation…

www.nowtheworld.com/ASDocs/StringUtility

Take Care.

_michael