This is not something strings should do; you should be using URL instead. Foundation realized this long ago and has started deprecating string-based path APIs because treating a path as a string exposes fundamental misconceptions very quickly.
> extract regex matches
This is something that is being worked on/discussed, but has not been “solved” yet.
I didn't mean that String should have a "pathExtension" property like NSString. I just want operations like "insert a number before the last period" to be easier to do.
Also, there are a lot more things that are a pain with String. I mean, just the fact that I can't pass a Substring to a function that expects a String boggles my mind.
Anyway, these types of operations come up all the time when working with strings, and these operations are really really incovenient in Swift.
Again, what are these operations? I think most of the issues people have with String is due to hard to discover APIs rather than design issues with String.
Swift has sweet solutions for a few special cases (eg. parse file names, parse dates, parse JSON), but parsing generic text is not something that Swift is good at.
NSString has a rich set of APIs for those tasks (NSScanner, NSRegularExpression, and all the rangeOfString... methods). Swift has pretty much nothing. (There is firstIndex(of:), but as far as I can see there is no way to find the next index, which makes it pretty much useless for most parsing tasks)
Maybe the problem is that I'm just not familiar enough with Swifts String, but until now every time I've had to work with Strings I became very frustrated.
Stuff.tar1.gz would like to have a word with you.
My input parsing code was inevitably a mess of stuff like:
let parts = l.components(separatedBy: ",")
let first = parts[0].components(separatedBy: "=")
let second = parts[1].trimmingCharacters(in: .whitespaces).components(separatedBy: "=")
When I did puzzles in D my input parsing was just creating a regex, matching on it, and pulling out the results.I started to look at how to do regex in Swift and it seemed easier (!) to just use .components(separatedBy:) as required. Google came up with a number of people who have created libraries/extensions that make this easier, but Swift really needs some better regex handling built in.
let parts = l.split(separator: ",").map{ $0.split(separator: "=") },
first = parts[0],
second = parts[1]https://developer.apple.com/documentation/foundation/scanner