TruMan1
TruMan1

Reputation: 36098

How to trim start and end of string in Swift?

I'd like to trim / from the beginning and end of a string in Swift. Is there anything built or for this or what's a good way to do this?

Upvotes: 2

Views: 523

Answers (1)

Eric Aya
Eric Aya

Reputation: 70098

I like to use stringByTrimmingCharactersInSet in such cases, with a custom character set (here just "/"):

let original = "/word/"
let result = original.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "/"))

Result: "word"

Upvotes: 1

Related Questions