How to get the filename from the filepath in swift

How to get the filename from the given file path string?

For example if I have a filepath string as

file:///Users/DeveloperTeam/Library/Developer/CoreSimulator/Devices/F33222DF-D8F0-448B-A127-C5B03C64D0DC/data/Containers/Data/Application/5D0A6264-6007-4E69-A63B-D77868EA1807/tmp/trim.D152E6EA-D19D-4E3F-8110-6EACB2833CE3.MOV

and I would like to get the return result as

trim.D152E6EA-D19D-4E3F-8110-6EACB2833CE3.MOV
105214 次浏览

目标 C

NSString* theFileName = [string lastPathComponent]

斯威夫特

let theFileName = (string as NSString).lastPathComponent

试试这个

let filename: String = "your file name"
let pathExtention = filename.pathExtension
let pathPrefix = filename.stringByDeletingPathExtension

更新:

extension String {
var fileURL: URL {
return URL(fileURLWithPath: self)
}
var pathExtension: String {
return fileURL.pathExtension
}
var lastPathComponent: String {
return fileURL.lastPathComponent
}
}

希望能有帮助。

斯威夫特2:

var file_name = NSURL(fileURLWithPath: path_to_file).lastPathComponent!

SWIFT 3.x or SWIFT 4: Shortest and cleanest way of doing this is below. In this example url variable is type of URL in this way we can have a human readable String result of the full file name with extension like My file name.txt and Not like My%20file%20name.txt

// Result like: My file name.txt
let fileName = url.lastPathComponent

如果您希望获得当前文件名,比如用于日志记录,我将使用以下命令。

Swift 4

URL(fileURLWithPath: #file).lastPathComponent

创建包含前两个文件夹的唯一“文件名”表单 url

func createFileNameFromURL (colorUrl: URL) -> String {


var arrayFolders = colorUrl.pathComponents


// -3 because last element from url is "file name" and 2 previous are folders on server
let indx = arrayFolders.count - 3
var fileName = ""


switch indx{
case 0...:
fileName = arrayFolders[indx] + arrayFolders[indx+1] + arrayFolders[indx+2]
case -1:
fileName = arrayFolders[indx+1] + arrayFolders[indx+2]
case -2:
fileName = arrayFolders[indx+2]
default:
break
}




return fileName

}

Below code is working for me in Swift 4.X

 let filename = (self.pdfURL as NSString).lastPathComponent  // pdfURL is your file url
let fileExtention = (filename as NSString).pathExtension  // get your file extension
let pathPrefix = (filename as NSString).deletingPathExtension   // File name without extension
self.lblFileName.text = pathPrefix  // Print name on Label

要从 Swift > = 4.2中的 URL 检索不带扩展名的文件名:

let urlWithoutFileExtension: URL =  originalFileUrl.deletingPathExtension()
let fileNameWithoutExtension: String = urlWithoutFileExtension.lastPathComponent

您可以在 fileUrl 中传递 url,如下所示:-

let fileUrl: String = "https://www.himgs.com/imagenes/hello/social/hello-fb-logo.png" // Pass the URL


let lastPathComponent = URL.init(string: fileUrl)?.lastPathComponent ?? "" // With this you will get last path component


let fileNameWithExtension = lastPathComponent

//最后一个路径组件将为您提供扩展名为 file Name 的文件。

let theURL = URL(string: "yourURL/somePDF.pdf")  //use your URL
let fileNameWithExt = theURL?.lastPathComponent //somePDF.pdf
let fileNameLessExt = theURL?.deletingPathExtension().lastPathComponent //somePDF

为了这个工作,你的网址必须是类型的 URL 不是一个字符串,所以不要把它转换成一个字符串之前。

你可以直接把这些代码复制粘贴到操场上,看看它是如何工作的。

这个比 URLNSString都快:

path.components(separatedBy: "/").last

我做了一些性能测试(iOS14,真正的设备,版本配置) :

(#file as NSString).lastPathComponent // The fastest option.


URL(string: #file)!.lastPathComponent // 2.5 times slower than NSString.


#file.components(separatedBy: "/").last! // 7 times slower than NSString.

意外收获:

URL(fileURLWithPath: #file, isDirectory: false).lastPathComponent // About the same as URL(string:).


URL(fileURLWithPath: #file).lastPathComponent // 2.5 times slower than with explicit isDirectory.