Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 373 Vote(s) - 3.4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to delete last path component of a String in Swift?

#1
I have a String `11/Passion/01PassionAwakening.mp3` and I need to delete the last path component `01PassionAwakening.mp3` in order to get `11/Passion`.

How can I do this while saving both components?
Reply

#2
You should really do away with legacy `NS` Objective-C classes and manual path string splitting where possible. Use `URL` instead:

let url = URL(fileURLWithPath: "a/b/c.dat", isDirectory: false)
let path = url.deletingLastPathComponent().relativePath // 'a/b'
let file = url.lastPathComponent // 'c.dat'

That being said, Apple has an explicit [FilePath](

[To see links please register here]

) type starting with macOS 11, but with no path manipulation methods. For those you'd have to include the external [system package](

[To see links please register here]

)

If you are on macOS 12, the methods from the external package are now also available on the system.
Reply

#3
**Swift 4+:**

let components = path.split(separator: "/")
let directory = components.dropLast(1).map(String.init).joined(separator: "/")

**Swift 3:**

let str = "11/Passion/01PassionAwakening.mp3"
if !str.isEmpty {
let components = str.characters.split("/")
let head = components.dropLast(1).map(String.init).joinWithSeparator("/")
let tail = components.dropFirst(components.count-1).map(String.init)[0]

print("head:",head,"tail:", tail) // head: 11/Passion tail: 01PassionAwakening.mp3
} else {
print("path should not be an empty string!")
}
Reply

#4
You can separate your url into two parts like so:


let str : NSString = "www.music.com/Passion/PassionAwakening.mp3"
let path : NSString = str.stringByDeletingLastPathComponent
let ext : NSString = str.lastPathComponent

print(path)
print(ext)

#Output

[To see links please register here]

PassionAwakening.mp3

For more info please have a look at [this link][1].


[1]:

[To see links please register here]

Reply

#5
Just improvised the solution for URL String. Thank you so much ingconti

extension String {

var ns: URL? {
return URL.init(string: self)
}

var pathExtension: String {
return ns?.pathExtension ?? ""
}

var lastPathComponent: String {
return ns?.lastPathComponent ?? ""
}

var stringByDeletingLastPathComponent: String {
return ns?.deletingLastPathComponent().absoluteString ?? ""
}
}

Reply

#6
rolling back to NSString:





extension String {

var ns: NSString {
return self as NSString
}

var pathExtension: String {
return ns.pathExtension
}

var lastPathComponent: String {
return ns.lastPathComponent
}

var stringByDeletingLastPathComponent: String {
return ns.deletingLastPathComponent
}

}


so you can do:

let folderPath = pathString.stringByDeletingLastPathComponent

Reply

#7
This works for Swift 3.0 as well:

let fileName = NSString(string: "11/Passion/01PassionAwakening.mp3").lastPathComponent
Reply

#8
Swift 3.0 version

if !str.isEmpty {
let components = str.characters.split(separator: "/")
let head = components.dropLast(1).map(String.init).joined(separator: "/")
let words = components.count-1
let tail = components.dropFirst(words).map(String.init)[0]

print("head:",head,"tail:", tail) // head: 11/Passion tail: 01PassionAwakening.mp3
} else {
print("path should not be an empty string!")
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through