Better String in Swift Using Stryng

20th December 2017

If you write in Swift, then there’s a good chance you’ve tried to access a section (Substring) of a String.

Usually it’s done by using an index, and an offset. But in some cases you need to specify the start index along with an offset, and also an end index with an offset.

It gets a bit messy.

Luckily I’ve just discovered Stryng on GitHub, and it’s a beautiful solution.

There’s a ton of examples, and you should totally read the README even if you aren’t going to use it. But here’s my favourite:

Before Stryng:

let message = “One Two Three”
message[message.index(message.startIndex, offsetBy: 4)..<message.index(message.startIndex, offsetBy: 7)] // “Two”

After Stryng:

let message = “One Two Three”
message[4..<7] // "Two”


By Chris Hannah. An app developer, blogger, and photographer, not sure what order. Say hello on, Twitter, or email.

✉️ Want to receive letters directly from me?

I send out one long-form letter to members every single month. No-nonsense, just a deep dive into a topic I'm interested in. No annoying tracking pixels, and it's completely free.

Become a Member