+ Start a Discussion
SRajesh8SRajesh8 

Split String by Comma but omit comma within Double quotes in apex

Hi All,

I have array of string : one,two,three,four,"five1,five2", six ,seven,"eight1,eight2","nine",,eleven

I need output as :
one
two
three
four
five1,five2
six
seven
eight1,eight2
nine

eleven

Thanks in advance
AvaneeshAvaneesh
Hi Rajesh

First, you have to remove all "  " with space then break it

String str = '\"First Name\",\"Last Name\"';
System.debug('===str==='+str);
for(String strFinal: str.replace('\"', '').split(','))
{
    System.debug('===Final val==='+strFinal);
} 

 I hope this was helpful don't forget to mark best 

Thank you
Avaneesh Singh
DeepthiDeepthi (Salesforce Developers) 

Hi Rajesh,

Kindly look into the below link that is similar to your requirement: https://stackoverflow.com/questions/11456850/split-a-string-by-commas-but-ignore-commas-within-double-quotes-using-javascript 

Hope this helps you!
Thanks,
Deepthi

D-CoderD-Coder
Solved it in apex :

Execute below code in Developer console and check debug statements for distinct items.
string s1 = 'one,two,three,four,"five1,five2", six ,seven,"eight1,eight2","nine",,eleven';
s1 = s1.trim();

List<String> lst = s1.split('"');
List<String> finalList = new List<String>();

for(String s : lst){
    if(s.countMatches(',') != 1){
        
        List<String> lst2 = new List<String>();
        lst2 = s.split(',');
        
        for(String s2 : lst2){
            
            finalList.add(s2);
        }
        
    }else{finalList.add(s);}
  
}

for(string s : finalList){
    s= s.trim();
    if(string.isNotBlank(s) && s != ','){
        
        system.debug('===============>'+s);
    }
    
}

If this answers your query please mark this question as a solved so that it can be filtered out from unsolved questions.
RD@SFRD@SF
Hi Rajesh,
By Array of string do you mean that "five1,five2" is one element in the array, if so can't you iterate the main array?
Or
Is it a single string which you would like to parse?

How are you generating this array?


Kind regards
karthikeyan perumalkarthikeyan perumal
Hello, 

Use below code
String strTest = 'one,two,three,four,"five1,five2", six ,seven,"eight1,eight2","nine",,eleven';

String[] arrTest = strTest.split('("[^"]*")');
String[] FinalarrTest =new List<String>(); 


static final Pattern doubleQuoteWrapped = Pattern.compile('("[^"]*")');

static List<String> parse(String input)
{
    Matcher m = doubleQuoteWrapped.matcher(input);
    List<String> output = new List<String>();
    while (m.find()) output.add(m.group());
      
    return output;
}
String[] DoubleQ= parse(strTest);
 
for(integer i=0; i<arrTest.size(); i++)
{
   FinalarrTest.add(string.valueof(string.valueof(arrTest[i]).split(','))); 
}
  


System.debug(FinalarrTest[0]);
System.debug(FinalarrTest[3]);
System.debug(DoubleQ[0]);
System.debug(DoubleQ[1]);
System.debug(DoubleQ[2]);

Ouput:  

User-added image

Hope this help you. 

feel free to ask me for any clarification. 

Thanks
karthik
 
SRajesh8SRajesh8
Hi Thanks to all.

string s1 = 'one,,three,four,"five1,five2",  ,seven,"eight1,eight2","nine","Ten,Ten1",eleven';
s1 = s1.trim();

List<String> lst = s1.split('\\"');
List<String> finalList = new List<String>();
system.debug('ttttttttt'+lst);
for(String s : lst){
    system.debug('ttttttttt'+s);
    system.debug('ttttttttt'+s.countMatches(','));
    if(s.countMatches(',') != 1){
        
        List<String> lst2 = new List<String>();
        lst2 = s.split(',');
        system.debug('===============>'+lst2);
        for(String s2 : lst2){
            
            finalList.add(s2);
        }
        
    }else{finalList.add(s);}
  system.debug('===============>'+finalList);
}

for(string s : finalList){
    //s= s.trim();
    if(s != ','){
        
        system.debug('===============>'+s);
    }
    
}

If I keep empty after "five1,five2" means in debug it placing extra space not 1 but 2, actually it should b e 1.
'one,,three,four,"five1,five2",  ,seven,"eight1,eight2","nine","Ten,Ten1",eleven';
19:24:01:003 USER_DEBUG [21]|DEBUG|===============>(one, , three, four, five1,five2,  ,   , seven, eight1,eight2........)
Output should be like : one, , three, four, five1,five2,  , seven, eight1,eight2, ....

Thanks In Advance
Akshay_DhimanAkshay_Dhiman
Hi SRajesh,
Try the code below,
        


       String str='';
       //String str2='';
       String [] arrayOfProducts = new List<String>();
       //Adding elements in Array
       arrayOfProducts.add('one');
       arrayOfProducts.add('two');
       arrayOfProducts.add('three');  
       arrayOfProducts.add('four');
       arrayOfProducts.add('"five1,five2"');
       arrayOfProducts.add('six');
       arrayOfProducts.add('seven');   
       arrayOfProducts.add('"eight1,eight2"');
       arrayOfProducts.add('nine');
        arrayOfProducts.add('');
         arrayOfProducts.add(‘eleven’);
      Integer si=0;
       for (Integer i = 0; i<arrayOfProducts.size(); i++)
   {
    si++;
    str=str+arrayOfProducts[i]+',,';  
}
List<String>stringhsr=new List<String>();
for(Integer i=1; i<=si; i++)
{
Stringhsr=str.split(',,');
}
for(String sf:Stringhsr)
{
system.debug('-->'+sf);
}
 
User-added image
Regards,
Akshay.