Splitting a String and using the second part only in java

I have a list of string like this:

private List<String> repositoryTitle;

this contains 3 values:

yashwantyj97/test,
yashwantyj97/brokencrystals,
yashwantyj97/petclinic

I want a list which contains these 3 values:

test, brokencrystals, petclinic

How to achieve this result dynamically?

>Solution :

Split them, and take the element at index 1.

repositoryTitle.stream()
   .map(str -> str.split("/")[1])
   .collect(Collectors.toList())

It will throw an exception if they don’t contain a slash, so you might want to filter them first.

Leave a Reply