简体   繁体   中英

swift3 - replace url

I should replace download URL like "../~~~~.pdf.2.3"(.zip, .xls etc...) to "../~~~.pdf"

If I use url.lastPathComponent, returning nil.

So I did like this code.

let fileLastPathComponents = remoteFileUrl.absoluteString.components(separatedBy: "/")
    let lastPathComponent = fileLastPathComponents[fileLastPathComponents.count - 1]
    let fileName = lastPathComponent.components(separatedBy: ".")
    let fileNameStr = "\(fileName[0]).\(fileName[1])"

It was worked but removingPercentEncoding not worked (return nil)

How can I bring encoded file name?

I cannot change this Server

Thank you

Will this solution be fine for you?

func dropVersion(fromPath path: String) -> String {
    var path = path
    var lastComponent = (path as NSString).lastPathComponent
    path = (path as NSString).deletingLastPathComponent as String

    while lastComponent.characters.count > 0 && (lastComponent.characters.last == "." || Int(String(lastComponent.characters.last!)) != nil) {
        lastComponent = String(lastComponent.dropLast())
    }

    return path + "/" + lastComponent
}

let path = "/this_is/your/path.zip.2.3"
dropVersion(fromPath: path) // will return /this_is/your/path.zip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM