Given a numeric string, I would like to apply an operation every nth digit in the string. Is this possible with java streams?
For example, for the string "12345"
and applying a sum of 3 on every 2nd character, the result would be "15375"
.
public static void main(String[] args) {
"12345".chars()
.mapToObj(Character::getNumericValue)
.mapToInt(c -> c + 3) //should only happen every 2nd character
.forEach(System.out::print);
}
The above results in 45678
because the sum is applied to all characters.