Returns everything before the last period, if any.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace( removeExtension( "document.jpg" ) ); // Displays: document
trace( removeExtension( "document" ) ); // Displays: document
trace( removeExtension( "document.1.jpg" ) ); // Displays: document.1
trace( extractExtension( "document.jpg" ) ); // Displays: .jpg
trace( extractExtension( "document" ) ); // Displays nothing
trace( extractExtension( "document.1.jpg" ) ); // Displays: .jpg
}
private function removeExtension( filename:String ):String {
var extensionIndex:Number = filename.lastIndexOf( '.' );
if ( extensionIndex == -1 ) {
return filename;
} else {
return filename.substr( 0, extensionIndex );
}
}
private function extractExtension( filename:String ):String {
var extensionIndex:Number = filename.lastIndexOf( '.' );
if ( extensionIndex == -1 ) {
return "";
} else {
return filename.substr( extensionIndex + 1, filename.length );
}
}
}
}
Related examples in the same category