ios 如何快速从文件路径中获取文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31780453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the filename from the filepath in swift
提问by Ralph
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
Thank you for your help.
感谢您的帮助。
回答by Sujith Chandran
Objective C
目标 C
NSString* theFileName = [string lastPathComponent]
Swift
迅速
let theFileName = (string as NSString).lastPathComponent
回答by Trevor
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
SWIFT 3.x 或 SWIFT 4:执行此操作的最短和最干净的方法如下。在这个例子中,url
变量是类型的URL
,这样我们就可以得到一个人类可读String
的完整文件名的结果,扩展名为 likeMy file name.txt
和 Not likeMy%20file%20name.txt
// Result like: My file name.txt
let fileName = url.lastPathComponent
回答by Jose Ramirez
If you want to get the current file name such as for logging purposes, I use this.
如果您想获取当前文件名(例如用于日志记录),我会使用它。
Swift 4
斯威夫特 4
URL(fileURLWithPath: #file).lastPathComponent
回答by Vingtoft
Swift 2:
斯威夫特 2:
var file_name = NSURL(fileURLWithPath: path_to_file).lastPathComponent!
回答by Pradumna Patil
Try this
尝试这个
let filename: String = "your file name"
let pathExtention = filename.pathExtension
let pathPrefix = filename.stringByDeletingPathExtension
Updated :
更新 :
extension String {
var fileURL: URL {
return URL(fileURLWithPath: self)
}
var pathExtension: String {
return fileURL.pathExtension
}
var lastPathComponent: String {
return fileURL.lastPathComponent
}
}
Hope it helps.
希望能帮助到你。
回答by Ashu
Below code is working for me in Swift 4.X
下面的代码在 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
回答by Nishant Sharma
You can pass the url in fileUrl, like I did below:-
您可以在 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
//This last path component will provide you file Name with extension.
//最后一个路径组件将为您提供带扩展名的文件名。
回答by Robac
let theURL = URL(string: "yourURL/somePDF.pdf") //use your URL
let fileNameWithExt = theURL?.lastPathComponent //somePDF.pdf
let fileNameLessExt = theURL?.deletingPathExtension().lastPathComponent //somePDF
In order for this to work your url must be of type URL not a string so don't convert it to a string before hand.
为了使其工作,您的 url 必须是 URL 类型而不是字符串,因此不要事先将其转换为字符串。
You can copy and paste this code directly into playground to see how it works.
您可以将此代码直接复制并粘贴到 Playground 中以查看其工作原理。
回答by Taiwosam
To retrieve filename without its extension from a URL in Swift >= 4.2:
在 Swift >= 4.2 中从 URL 检索没有扩展名的文件名:
let urlWithoutFileExtension: URL = originalFileUrl.deletingPathExtension()
let fileNameWithoutExtension: String = urlWithoutFileExtension.lastPathComponent
回答by Pvlub
Creates unique "file name" form url including two previous folders
创建唯一的“文件名”表单 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
}
}