Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove suffix from filename in Swift

When trying to remove the suffix from a filename, I'm only left with the suffix, which is exactly not what I want.

What (how many things) am I doing wrong here:

let myTextureAtlas = SKTextureAtlas(named: "demoArt")

let filename = (myTextureAtlas.textureNames.first?.characters.split{$0 == "."}.map(String.init)[1].replacingOccurrences(of: "\'", with: ""))! as String

print(filename)

This prints png which is the most dull part of the whole thing.

like image 393
Confused Avatar asked Oct 06 '16 04:10

Confused


2 Answers

If by suffix you mean path extension, there is a method for this:

let filename = "demoArt.png"
let name = (filename as NSString).deletingPathExtension
// name - "demoArt"
like image 72
Jovan Stankovic Avatar answered Nov 03 '22 00:11

Jovan Stankovic


If you don't care what the extension is. This is a simple way.

let ss = filename.prefix(upTo: fileName.lastIndex { $0 == "." } ?? fileName.endIndex))

You may want to convert resulting substring to String after this. With String(ss)

like image 23
possen Avatar answered Nov 03 '22 01:11

possen