I have checked all the posts regarding string and substrings but I can't find any solution for my problem. Say I have a string "ABCD/EFG" Or "A/Ef". Now I want to get all the characters after "/" and I want to do it by only start reading from the last index. (Output: "EFG", "Ef"). Thanks
Asked
Active
Viewed 181 times
1
-
You mean all the characters _after_ "/"? (your question is quite confusing, you say "before" and the output is the last component) – Alladinian May 20 '20 at 21:12
-
Yes. I am saying 'before' as if we start reading from the last index, then when we read character ''/', then I want all the characters before it. – Fahad Ali May 20 '20 at 21:14
-
Like, In 1st example, I ll start reading from G, then F, then E and when I ll read '/' , I want to display EFG – Fahad Ali May 20 '20 at 21:17
-
You're overthinking your problem. `yourString.split(separator: "/").last` is all you need. This produces an optional string, as there's no guarantee the splitted string array contains anything. – Sander Saelmans May 20 '20 at 21:21
-
Sorry if I forgot to mention that the string I am dealing with looks like this. 'Hotel/IsgxhSnhSSXjHvcseRrVOqseyvo1/IsgxhSnhSSXjHvcseRrVOqseyvo1Hotel/Hotel12'. So split cant be applied I think – Fahad Ali May 20 '20 at 21:24
-
@FahadAli Why not... works just fine – Alladinian May 20 '20 at 21:25
-
It can be aplied, it only produces an array with more than 1 result: ["Hotel", "IsgxhSnhSSXjHvcseRrVOqseyvo1", ... , "Hotel12"]. It looks like this is some sort of url however, you might want to go with the URL approach suggested by @Alladinian – Sander Saelmans May 20 '20 at 21:26
-
The string mentioned above has the last part Hotel12. I just want to take that out. And also, that last part can change like some strings can have Room12 instead. Also, from the beginning, the String length can be varied. – Fahad Ali May 20 '20 at 21:27
2 Answers
0
You got a few options:
- Split on "/" and keep the last component:
let text = "ABCD/EFG"
if text.contains("/"), let last = text.components(separatedBy: "/").last {
print(last) // => EFG
}
- If your strings are URLs (or have similar structure) you can make
URL
do it for you:
let text = "A/Ef"
if text.contains("/"), let last = URL(string: text)?.lastPathComponent {
print(last) // => Ef
}
Note: I'm only checking for "/" presence to avoid returning matches on strings that don't have a slash. If you are parsing URLs then you probably do not need this check.

Alladinian
- 34,483
- 6
- 89
- 91
0
You can use String lastIndex(of:)
method to find the last occurrence of your slash character, get the index after that index limited by the string endIndex and get the substring from that index forward:
let str = "ABCD/EFG"
if let lastIndex = str.lastIndex(of: "/"),
let index = str.index(lastIndex, offsetBy: 1, limitedBy: str.endIndex) {
let substring = str[index...] // "EFG"
// if you need a string
let string = String(str[index...]) // "EFG"
}
or as suggested by @MartinR using string range(of:)
(this needs Foundation framework):
if let index = str.range(of: "/", options: .backwards)?.upperBound {
// ...
}

Leo Dabus
- 229,809
- 59
- 489
- 571
-
1With `range(of:)` you save the index correction: https://stackoverflow.com/a/44982892/1187415 :) – Martin R May 20 '20 at 21:41
-
@MartinR yes I didn't think about using `range(of:)` but thats definitely an option – Leo Dabus May 20 '20 at 21:42
-
1At first, URL worked for me, but my string was not a URL and contained some spaces due to which the URL solution wasn't working. This one is working great according to my requirment. – Fahad Ali May 20 '20 at 21:45
-
@FahadAli: Spaces should not be a problem if you use `URL(fileURLWithPath:)` – Martin R May 20 '20 at 22:00