如何从给定的文件路径字符串中获取文件名?
例如,如果我有一个文件路径字符串
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
Run Code Online (Sandbox Code Playgroud)
我希望得到返回结果
trim.D152E6EA-D19D-4E3F-8110-6EACB2833CE3.MOV
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助.
小智 145
目标C.
NSString* theFileName = [string lastPathComponent]
Run Code Online (Sandbox Code Playgroud)
迅速
let theFileName = (string as NSString).lastPathComponent
Run Code Online (Sandbox Code Playgroud)
Tre*_*vor 27
SWIFT 3.x或SWIFT 4:最简单,最干净的方法如下.在这个示例中,url变量是这样的类型,URL我们可以String使用扩展名为My file name.txt和不喜欢的完整文件名的人类可读结果My%20file%20name.txt
// Result like: My file name.txt
let fileName = url.lastPathComponent
Run Code Online (Sandbox Code Playgroud)
Vin*_*oft 23
斯威夫特2:
var file_name = NSURL(fileURLWithPath: path_to_file).lastPathComponent!
Run Code Online (Sandbox Code Playgroud)
Jos*_*rez 23
如果您想获取当前文件名,例如用于记录目的,我会使用它.
斯威夫特4
URL(fileURLWithPath: #file).lastPathComponent
Run Code Online (Sandbox Code Playgroud)
小智 14
let theURL = URL(string: "yourURL/somePDF.pdf") //use your URL
let fileNameWithExt = theURL?.lastPathComponent //somePDF.pdf
let fileNameLessExt = theURL?.deletingPathExtension().lastPathComponent //somePDF
Run Code Online (Sandbox Code Playgroud)
为了使其工作,您的 url 必须是 URL 类型而不是字符串,因此不要事先将其转换为字符串。
您可以将此代码直接复制并粘贴到 Playground 中以查看其工作原理。
Pra*_*til 12
试试这个
let filename: String = "your file name"
let pathExtention = filename.pathExtension
let pathPrefix = filename.stringByDeletingPathExtension
Run Code Online (Sandbox Code Playgroud)
更新 :
extension String {
var fileURL: URL {
return URL(fileURLWithPath: self)
}
var pathExtension: String {
return fileURL.pathExtension
}
var lastPathComponent: String {
return fileURL.lastPathComponent
}
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
下面的代码在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
Run Code Online (Sandbox Code Playgroud)
我做了一些性能测试(iOS 14,真实设备,发布配置):
(#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.
Run Code Online (Sandbox Code Playgroud)
奖金:
URL(fileURLWithPath: #file, isDirectory: false).lastPathComponent // About the same as URL(string:).
URL(fileURLWithPath: #file).lastPathComponent // 2.5 times slower than with explicit isDirectory.
Run Code Online (Sandbox Code Playgroud)
您可以在 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
Run Code Online (Sandbox Code Playgroud)
//最后一个路径组件将为您提供带扩展名的文件名。
| 归档时间: |
|
| 查看次数: |
58826 次 |
| 最近记录: |