How to Split Filename from File Extension in Swift

How to split filename from file extension in Swift?

This is with Swift 2, Xcode 7: If you have the filename with the extension already on it, then you can pass the full filename in as the first parameter and a blank string as the second parameter:

let soundURL = NSBundle.mainBundle()
.URLForResource("soundfile.ext", withExtension: "")

Alternatively nil as the extension parameter also works.

If you have a URL, and you want to get the name of the file itself for some reason, then you can do this:

soundURL.URLByDeletingPathExtension?.lastPathComponent

Swift 4

let soundURL = NSBundle.mainBundle().URLForResource("soundfile.ext", withExtension: "")
soundURL.deletingPathExtension().lastPathComponent

Swift split string to get file type from URL

Use the pathExtension property after contructing a URL from your initial string:

let string = "Desktop/exampleFiles/targetFile.png"

let url: URL? = URL(string: string)
let urlExtension: String? = url?.pathExtension

Swift get file extension from web link

You can do it by using NSString with pathExtension for the file extension:

let url: NSString = "http://hr-platform.nv5.pw/image/comp_1/pdf-test.pdf" // http://hr-platform.nv5.pw/image/comp_1/pdf-test.pdf
let fileExtension = url.pathExtension // pdf
let urlWithoutExtension = url.deletingPathExtension // http://hr-platform.nv5.pw/image/comp_1/pdf-test

As @David Berry suggested use the URL-class:

let url = URL(string: "http://hr-platform.nv5.pw/image/comp_1/pdf-test.pdf")
let fileExtension = url?.pathExtension // pdf
let fileName = url?.lastPathComponent // pdf-test.pdf

Remove suffix from filename in Swift

You can also split the String using componentsSeparatedBy, like this:

let fileName = "demoArt.png"
var components = fileName.components(separatedBy: ".")
if components.count > 1 { // If there is a file extension
components.removeLast()
return components.joined(separator: ".")
} else {
return fileName
}

To clarify:

fileName.components(separatedBy: ".")

will return an array made up of "demoArt" and "png".

How to get the filename from the filepath in swift

Objective C

NSString* theFileName = [string lastPathComponent]

Swift

let theFileName = (string as NSString).lastPathComponent

Print filename in extension

Assuming that you want to log file name and line number of the caller of that method, this is the only way. Compare Expressions (emphasis added)

When used as the default value of a function or method parameter, the special literal’s value is determined when the default value expression is evaluated at the call site.

In your case:

// Function defined in file "A.swift"
func pinToSuperview(file: String = #file) { print(file) }

// Function called in file "B.swift"
pinToSuperview()

would print "B.swift". On the other hand, #file inside the function body expands to the file name where the function is defined, so

// Function defined in file "A.swift"
func pinToSuperview() { print(#file) }

// Function called in file "B.swift"
pinToSuperview()

would print "A.swift".

Replace extension of a filename in swift

stringByAppendingPathExtension no longer works in Swift 2.0 (if you are using String). But it does still work for NSString. So you can do:

var storePath = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory , .UserDomainMask, true)[0]) as NSString
storePath = storePath.stringByAppendingPathExtension("model.sqlite")!

iOS Swift, how to do if statement for different file extension?

if (fileURL.pathExtension == "png") {
// image
} else if (fileURL.pathExtension == "mp4") {
// video
}

Objective-C: Extract filename from path string

Taken from the NSString reference, you can use :

NSString *theFileName = [[string lastPathComponent] stringByDeletingPathExtension];

The lastPathComponent call will return thefile.ext, and the stringByDeletingPathExtension will remove the extension suffix from the end.



Related Topics



Leave a reply



Submit