如何在ActionScript中将文件名与文件夹分开?

时间:2012-05-16 14:04:08

标签: actionscript-3

如何在ActionScript中将文件名与字符串中的文件夹分开?它应该拆分变量并将其保存在我稍后可以使用的两个单独的变量中。

我想我必须使用正则表达式,但我对正则表达式并不擅长。

例如:

var filepath = "/Users/folder1/folder2/test.zip";

如何将其分成这个?:

 var filename = "test.zip";
 var path = "/Users/folder1/folder2/";

2 个答案:

答案 0 :(得分:3)

非正则表达式可能是这样的:

var fullpath:String = "/Users/folder1/folder2/test.zip";
var arr:Array = fullpath.split("/");
var filename:String = arr.pop();
var path:String = arr.join("/") + "/";

trace(filename); // outputs test.zip
trace(path); // outputs /Users/folder1/folder2/

答案 1 :(得分:0)

或没有数组:

var fullpath:String = "/Users/folder1/folder2/test.zip";
var filename:String = fullpath.substr(fullpath.lastIndexOf("/")+1,fullpath.length);
var filepath:String = fullpath.substr(0,fullpath.lastIndexOf("/")+1);
trace("filename = " + filename);
trace("filepath = " + filepath);
trace("fullpath = " + fullpath);

/*
filename = test.zip
filepath = /Users/folder1/folder2/
fullpath = /Users/folder1/folder2/test.zip
*/