Splitting Strings in Flex ActionScript: split method


Using Split method on a given String, you can get an array of Substrings. Each substring is formed from the given String by spliting them according to another given string, or expression. That means, the another string separates or splits the mother-String and split it into array of Substrings.

Example:

var myStr:String = "first_second_third";
var subStr:Array = myStr.split("_");

Now subStr[0] will contain “first”, subStr[2] will contain “third”;

More Practical Example:

package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var filename:String = "document.jpg";
var nameParts:Array = filename.split(".");
var extensionless:String = nameParts[0];
trace ("The filename is " + extensionless);
var extension:String = nameParts[1];
trace ("The file extension is " + extension);
}
}
}

You can see following Useful Links:

http://www.java2s.com/Code/Flash-Flex-ActionScript/String/Usesplitmethodtogetthefilenameanditsextensionname.htm

This article was previously published at my Progmaatic Blog.

Advertisements