I have strings in the below pattern:
ab:ab:ab:1:ab
ac:ac:ac:2:ac
ad:ad:ad:3:ad
I have to extract the string between the 3rd colon
and the 4th colon
.
For the above example strings, the results would be "1"
, "2"
and "3"
.
What is a short way in Java using string functions to extract it?
You can just split by ":" and get the fourth element:
something like:
Stream.of("ab:ab:ab:1:ab", "ac:ac:ac:2:ac", "ad:ad:ad:3:ad")
.map(s -> s.split(":")[3])
.forEach(System.out::println);
Output:
1
2
3