Skip to content

Instantly share code, notes, and snippets.

@lkatney
Last active September 13, 2017 16:25
Show Gist options
  • Save lkatney/d304403badbd9ccaf2dead4bdc20b62e to your computer and use it in GitHub Desktop.
Save lkatney/d304403badbd9ccaf2dead4bdc20b62e to your computer and use it in GitHub Desktop.
String csvLine = 'Test,Check,abc@abc.com, "19, Link road, A1SH10, India", companyName, "companyStreet, CompanyCity, CompanyCountry"';
String prevLine = csvLine;
Integer startIndex;
Integer endIndex;
while(csvLine.indexOf('"') > -1){
if(startIndex == null){
startIndex = csvLine.indexOf('"');
csvLine = csvLine.substring(0, startIndex) + ':quotes:' + csvLine.substring(startIndex+1, csvLine.length());
}else{
if(endIndex == null){
endIndex = csvLine.indexOf('"');
csvLine = csvLine.substring(0, endIndex) + ':quotes:' + csvLine.substring(endIndex+1, csvLine.length());
}
}
if(startIndex != null && endIndex != null){
String sub = csvLine.substring(startIndex, endIndex);
sub = sub.replaceAll(',', ':comma:');
csvLine = csvLine.substring(0, startIndex) + sub + csvLine.substring(endIndex, csvLine.length());
startIndex = null;
endIndex = null;
}
}
System.debug('prevLine:::'+prevLine);
System.debug('csvLine:::'+csvLine);
for(String column : csvLine.split(',')){
column = column.replaceAll(':quotes:', '').replaceAll(':comma:', ',');
System.debug('column::'+column);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment