For example i have String of "version/ofswift/developer".
and i want print which starts from back side until the specific index like my specific index will be "/" then the answer should be "developer". What code snippet would do that, and being the most swift-like? I can't find any good practice. I hope you will help me to figure it out. Thank In Advance.
Asked
Active
Viewed 266 times
0

Visal Sambo
- 1,270
- 1
- 19
- 33
-
show us what have you tried? past your code – Pratik Prajapati May 23 '18 at 13:28
-
There is a function something like “separatedBy” but I can’t think what it is right now. Take a look at String docs for Swift. – Fogmeister May 23 '18 at 13:28
-
@Pratik i have tried with subStrig method. But it is not like i am expected. – Visal Sambo May 23 '18 at 13:42
-
@Fogmeister yes, i will be checking with that. – Visal Sambo May 23 '18 at 13:42
-
That looks like a file path – in that case you should `URL` and `lastPathComponent` – Martin R May 23 '18 at 13:45
-
This might also be helpful: https://stackoverflow.com/q/44883983/1187415. – Martin R May 23 '18 at 13:46
-
@MartinR Obviously what i am looking for. Thank you – Visal Sambo May 23 '18 at 13:52
1 Answers
1
Use String method components(separatedBy:)
to produce an array and then get the last element using last
let string = "version/ofswift/developer"
let lastComponent = string.components(separatedBy: "/").last

Aleksander Maj
- 243
- 3
- 13