Get Odd and Even Numbers between two strings:
List<Integer> theEvenNumbers = new List<Integer>();//replace with Set<Integer> theEvenNumbers = new Set<Integer>(); if you need unique number or values
List<Integer> theOddNumbers = new List<Integer>();//replace with Set<Integer> theOddNumbers = new Set<Integer>(); if you need unique number or values
String s1 = '1,2,3,4,5,6,7,8';
String s2 = '2,4,5,6,8,10,12';
String result = s1 + ',' + s2 ;
for(String s : result.split(',')){
Integer i = Integer.valueOf(s);
if(Math.mod(i, 2) == 0){
theEvenNumbers.add(i);
}
else{
theOddNumbers.add(i);
}
}
system.debug('EVEN'+theEvenNumbers);
system.debug('ODD'+theOddNumbers);
List<Integer> theOddNumbers = new List<Integer>();//replace with Set<Integer> theOddNumbers = new Set<Integer>(); if you need unique number or values
String s1 = '1,2,3,4,5,6,7,8';
String s2 = '2,4,5,6,8,10,12';
String result = s1 + ',' + s2 ;
for(String s : result.split(',')){
Integer i = Integer.valueOf(s);
if(Math.mod(i, 2) == 0){
theEvenNumbers.add(i);
}
else{
theOddNumbers.add(i);
}
}
system.debug('EVEN'+theEvenNumbers);
system.debug('ODD'+theOddNumbers);
Comments
Post a Comment