Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get substring between 3rd colon and 4th colon

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".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What is a short way in Java using string functions to extract it?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading