+ Start a Discussion
Awe AweAwe Awe 

apex extra or split the string

hi how can i achieve this
//id
String a = '111,112,113,114 etc';

//my ouput should like this

String Result1 = 111,112,113
String Result2 = 113,114 etc

 
Best Answer chosen by Awe Awe
mukesh guptamukesh gupta
HI Awe,

Please use below code:-
String a = '111,112,113,114 etc';

String[] lst = a.split(',');

String result1;
String result2;
System.debug('lst.size()== '+lst.size());
for(Integer i = 0; i<lst.size() ; i++){
    if(i < 3){
        if(i == 0){
            result1 =  lst[i]; 
        }
        if(i> 0 && i<3){
            result1 +=  ','+lst[i];
        }
        if( i== 2){
            result2 =  lst[i];
        }
        
        
    }else{
        if(i== 3){
            result2 +=  ','+lst[i];
        } 
              
    }

}
system.debug('result1 '+result1);
system.debug('result2 '+result2);
if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 

All Answers

mukesh guptamukesh gupta
HI Awe,

Please use below code:-
String a = '111,112,113,114 etc';

String[] lst = a.split(',');

String result1;
String result2;
System.debug('lst.size()== '+lst.size());
for(Integer i = 0; i<lst.size() ; i++){
    if(i < 3){
        if(i == 0){
            result1 =  lst[i]; 
        }
        if(i> 0 && i<3){
            result1 +=  ','+lst[i];
        }
        if( i== 2){
            result2 =  lst[i];
        }
        
        
    }else{
        if(i== 3){
            result2 +=  ','+lst[i];
        } 
              
    }

}
system.debug('result1 '+result1);
system.debug('result2 '+result2);
if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
This was selected as the best answer
Awe AweAwe Awe
thanks for your help 
Awe AweAwe Awe
Hi

can possible the if else is dynamic base on the length lst size every 3..?

The string result will also automatic increment

can this possible?