Check out Miniroll.app, a way to create, share, and explore blogrolls!

Better String in Swift Using Stryng

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”

🤩

// Transmit

Send a short message straight to my phone. Plain text, optional name, one-way — no reply guaranteed.