I have this list
list = [M111111:Example, B22222:Example Extended, T33333:Example]
And I need to get only the code before the ":" for example:
list = [M111111, B22222, T33333]
I tried using the split function but it doesn't work.
CodePudding user response:
Assuming that your array consists of strings like this:
var list = ["M111111:Example", "B22222:Example Extended", "T33333:Example"];
you could use a loop:
for (let i = 0; i < list.length; i ) {
list[i] = list[i].slice(0, list[i].indexOf(":"));
}
CodePudding user response:
List<String> firstPart = Stream.of(list).map(s -> s.split(":")[0]).toList();
This will create a Stream of your list and for each element, split it on the :
and return the first element, then collect that back into a List<String>
that will contain, M111111, B22222, T33333, ...
CodePudding user response:
you can do something like this:
List<String> newList = new ArrayList<>();
list.forEach(s-> newList.add(s.substring(0,s.indexOf(":")));