2

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

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

例如:

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

我怎样才能把它分成这个?:

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

2 回答 2

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/
于 2012-05-16T14:13:04.003 回答
0

或者没有 Array :

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
*/
于 2012-05-17T13:52:51.483 回答