Featured Post

How To Upgrade Your MacBook Hard Drive – Part 2

I left off in Hard Drive Upgrade with an external drive prepared and ready to receive an image of my existing primary drive.  That article discussed backup strategies, a list of items needed in order to perform the data migration and what is needed in order to get started. We then stepped through the process of formatting and partitioning an external drive. In this article, we are going to go through the process of using SuperDuper. See the rest of my article on Lost In Technology. Share this on del.icio.us Digg this! Share this on Reddit Buzz up! Stumble upon something good? Share it on StumbleUpon Share this on Technorati Post this to MySpace Share this on Facebook Tweet This! Email this to a friend? Share this on Linkedin Seed this on Newsvine Add this to Google Bookmarks Share this on FriendFeed Submit this to Twittley

Read More

Follow @dougrdotnet on Twitter

Reader Question – Manipulating a String

Posted by dougr | Posted in ActionScript, Flex | Posted on 14-06-2009 | 1,979 views

Tags: , ,

0

A reader asked me a question about taking a date stored as a string and manipulating it so as to remove sub-strings. This is really simple in ActionScript, using the replace method. The replace() method is used to find a match in a particular string and then replace it with some other string inside the parameters of the method, such as string.replace(pattern, repl);

From Livedocs ( http://livedocs.adobe.com/flex/201/langref/String.html#replace() )

replace(pattern:*, repl:Object):String
Matches the specifed pattern against the string and returns a new string in which the first match of pattern is replaced with the content specified by repl.

The reader needed to take a date formatted as such – 15:07:47.001.850.000 and end up with a string formtted like the following – 50747001850.
As I said this is really simple using replace and just a little bit of RegEx:



private function init():void {

    var dateStr:String = "15:07:47.001.850.000";

    var d:String = stringReplace(dateStr);

    trace(d); // Output is 150747001850

}


    private function stringReplace(value:String):String {
    var v:String = value;
    v = v.replace(/:/g, "");
    v = v.replace(/\./g, "");
    var re:RegExp = new RegExp("000$");
    v = v.replace(re, "");

    return v;
}

In the init method, I’ve set the variable dateStr to the value of the string that needs to be manipulated. Next, I’ve set the variable d to the value of the result of the stringReplace() method which follows, passing in the original string. In the stringReplace() method, which accepts the string parameter named value, I assign value to the variable v. Next, using the replace() method, I search the string for any ‘:’ characters. The reason it will search the entire string is that I’ve used the global (g) flag in the expression. Then note that the second parameter of the replace method I’ve specified an empty string. This will replace any ‘:’ with “” (nothing). I’ve done the same in the second replace() method, only removing periods from the string and replacing with an empty string. The next line I create a new RegExp object so that I can use the following RegExp(”000$”) in order to remove the trailing 000. The $ in the expression applies the pattern match to the end of the string, so as to not remove any instances of 000 that might exist anywhere other than the end of string. This way if the original string is provided from a dynamic source there is no worry of removing zeros anywhere other than the end of string. I then run the replace() method a last time and apply the RegExp as the pattern and replace it with an empty string. When I return the modified string the result is the formatted string as needed for other parts of the application, shown in trace output.

Write a comment