You need to sign in to do that
Don't have an account?

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
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
First, you have to remove all " " with space then break it
I hope this was helpful don't forget to mark best
Thank you
Avaneesh Singh
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
Execute below code in Developer console and check debug statements for distinct items.
If this answers your query please mark this question as a solved so that it can be filtered out from unsolved questions.
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
Use below code
Ouput:
Hope this help you.
feel free to ask me for any clarification.
Thanks
karthik
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
Try the code below,
Regards,
Akshay.