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?
Asked
Active
Viewed 478 times
2

TruMan1
- 33,665
- 59
- 184
- 335
-
1possible duplicate of [Does swift has trim method on String?](http://stackoverflow.com/questions/26797739/does-swift-has-trim-method-on-string) – André Fratelli Jul 15 '15 at 21:05
-
Use the link provided by @AndréFratelli...but, here's a regex that would match what you want: [`/^\/+|\/+$/g`](https://regex101.com/r/wW2iP4/1) – Sam Jul 15 '15 at 21:08
-
Can you able to share your String code function. – Manikandan D Jul 15 '15 at 21:16
1 Answers
1
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"

Eric Aya
- 69,473
- 35
- 181
- 253