How to get the filename from the filepath in swift

后端 未结 10 1475
一生所求
一生所求 2020-12-04 20:47

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/Co         


        
相关标签:
10条回答
  • 2020-12-04 21:03

    Creates unique "file name" form url including two previous folders

    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
    

    }

    0 讨论(0)
  • 2020-12-04 21:06

    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.

    0 讨论(0)
  • 2020-12-04 21:06
    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.

    You can copy and paste this code directly into playground to see how it works.

    0 讨论(0)
  • 2020-12-04 21:10

    You can pass the url in fileUrl, like I did below:-

    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.

    0 讨论(0)
  • 2020-12-04 21:16

    To retrieve filename without its extension from a URL in Swift >= 4.2:

    let urlWithoutFileExtension: URL =  originalFileUrl.deletingPathExtension()
    let fileNameWithoutExtension: String = urlWithoutFileExtension.lastPathComponent
    
    0 讨论(0)
  • 2020-12-04 21:17

    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
    
    0 讨论(0)
提交回复
热议问题