Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do substring in some elements of string list using lambda

Below is my list of String.

["sunday", "monday", "tuesday", "wednesday", "fri", "satur"]

I want to do remove "day" from the elements if it is ending with "day". How to do this in Lambda ?

Expected Output in the list:

["sun", "mon", "tues", "wednes", "fri", "satur"]

I have tried the below code, but unable to assign the value to the list

daysList.stream().forEach(s -> { if(s.endsWith("day")) {
        s = s.substring(0, s.indexOf("day"));
    }});

Can anyone please help me on this ?

like image 424
Pavan Avatar asked Nov 29 '22 10:11

Pavan


1 Answers

Most of the answers here make use of a Stream, but you should not be using a Stream at all:

daysList.replaceAll(s -> s.replaceFirst("day$", ""));
like image 75
VGR Avatar answered Dec 04 '22 10:12

VGR