简体   繁体   中英

how to get the rest of path from sub folder

I have a path like:

file:///private/var/mobile/Containers/Data/Application/80C93A3C-DFB5-4756-8DC8-7ED34469EA11/Documents/allFiles/81B5D904-CCEC-4B92-BD9B-4175BF6A99BC.jpeg

The thing that I am looking for is to have the path value after documents path:

file:///private/var/mobile/Containers/Data/Application/80C93A3C-DFB5-4756-8DC8-7ED34469EA11/Documents

like give me the path value after the documents folder: the response should be:

allFiles/81B5D904-CCEC-4B92-BD9B-4175BF6A99BC.jpeg

I didn't find API about this.

If you have a String path then convert it into an URL,

let path = "file:///private/var/mobile/Containers/Data/Application/80C93A3C-DFB5-4756-8DC8-7ED34469EA11/Documents/allFiles/81B5D904-CCEC-4B92-BD9B-4175BF6A99BC.jpeg"
let url = URL(fileURLWithPath: path)

then split the url up into components

let components = url.pathComponents

and create a string from everything after the component "Documents"

if let index = components.firstIndex(of: "Documents") {
    print(components[components.index(index, offsetBy: 1)..<components.endIndex].joined(separator: "/"))
}

output

allFiles/81B5D904-CCEC-4B92-BD9B-4175BF6A99BC.jpeg

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