This is not a blog. So sue me!


Crikey, things are looking up!
Showing posts with label actionscript 2. Show all posts
Showing posts with label actionscript 2. Show all posts

Wednesday, October 15, 2008

ActionScript 2; replace string any number of times

The cheeseparing number of useful String class methods released by Adobe leaves something for the programmer to do. Here is a replace function that will replace a substring within another string any number of times. It starts at the first occurrence.



// replace findThis string from stringIn with replaceWith, repeat number of times
//repeat must be >=1, anything else it will do 1 repeat anyway
// if the string doesn't occur nothing will happen

public static function replaceString(stringIn:String, findThis:String, replaceWith:String, repeat:Number):String {

var stringOut:String = '';
var tempArr:Array;
tempArr = stringIn.split(findThis); //spilt the string into an array based on the occurrences of the target string
stringOut = tempArr[0]; //get the first split item
for (var i=1;i < tempArr.length;i++){
if (i < repeat+1){
stringOut = stringOut + replaceWith + tempArr[i];// replace the next occurrence and add the next split item
}
else {
stringOut = stringOut + findThis + tempArr[i];// restore the rest of the string as it was
}
}
trace("String in: " + stringIn + " String out: " + stringOut);
return stringOut;
}

Saturday, January 12, 2008

Passing values between Flash AS2 and JSP/HTML

We often use a Flash movie embedded in a JSP page to do something like make a nice dynamic report with media server features, for example.

To pass information to the Flash movie, there are several options:
1. Pass it in the movie load invocation: thing.swf?param1=value1 etc
2. Pick up the values you want from Javascript functions on the host page.
3. Collect the values you want from the server directly - send a request for xml data (for example) from the Flash movie, without reloading the page.

To pass information back out of the Flash movie:
Call a page Javascript function to set a value in the form
Send a post of data back to the server directly out of Flash.

(In preparation)

Thursday, November 15, 2007

getDateString: ActionScript 2.0 function to make a readable numeric date string

The getDateString returns the present date/time in the form of a readable (just) string down to milliseconds e.g. 20061231135901999. It uses the leftFill function below:

//get the current date/time and make a string
//in the form yyyymmddHHMMSSmmm
private function getDateString():String
{
var today:Date = new Date();
var dateString:String = today.getFullYear().toString()
+ leftFill((today.getMonth()+1).toString(),"0",2)
+ leftFill(today.getDate().toString(),"0",2)
+ leftFill(today.getHours().toString(),"0",2)
+ leftFill(today.getMinutes().toString(),"0",2)
+ leftFill(today.getSeconds().toString(),"0",2)
+ leftFill(today.getMilliseconds().toString(),"0",3);
return dateString;

}//getDateString

// This function will take a string (1-3 characters)
// and right justify it with fill characters padded to the left for the width required.
// Designed for use with Date/time fields 1, 2 or 3 in length
// if it is called on the wrong size inString it will return the correct size field of fill chars
private function leftFill(inString:String, fillChar:String, fieldWidth:Number):String
{
//make sure the fill character is only one character long!
var fill = fillChar.substr(0,1);
if (inString.length == fieldWidth)
{
return inString;
}
else if (inString.length == fieldWidth-1)
{
return fill + inString;
}
else if (inString.length == fieldWidth-2)
{
return fill + fill + inString;
}
else
{
return ((fill + fill + fill).substr(0,fieldWidth));
}
}//leftFill

removeString: ActionScript 2.0 function to remove delimited strings

This function is quite useful.
It will remove strings that are delimited by known characters, any number of times from a target string. For example to remove all the markups in an XML node and just leave the text, call it with this snippet:
tempString = pNode.toString();
while (tempString.indexOf("<") != -1) {
tempString = removeString(tempString,"<",">");
}

function removeString(input:String, sDelim:String, 
eDelim:String):String {
var startIndex:Number = 0;
var sDelimIndex:Number = input.indexOf(sDelim);
var eDelimIndex:Number = input.indexOf(eDelim);
var endIndex:Number = input.length-1;

var output:String = "";

if (input == undefined ||
input == "" ||
sDelimIndex != -1) {
//get the substring before the delimiter, if any
if (startIndex < sDelimIndex) {
output += input.substring(startIndex,sDelimIndex);
}
if (eDelimIndex != -1) { // if any end delim
// get the substring after the delimiter, if any
if (eDelimIndex < endIndex) {
output += input.substring(eDelimIndex+1,endIndex+1);
}
}
}
else {
//nothing to be done
output = input;
}
return output;
}

Contributors

St Lawrence Rowing

Test content from SLRC