Skip to content

Instantly share code, notes, and snippets.

@surajp
Created April 10, 2020 00:12
Show Gist options
  • Save surajp/f0fc315cf3ef4a347600451420ba1a95 to your computer and use it in GitHub Desktop.
Save surajp/f0fc315cf3ef4a347600451420ba1a95 to your computer and use it in GitHub Desktop.
CSV Column Iterator
public class CSVColIterator implements Iterator<String> {
private String colDelimiter=',';
private String textQualifier='"';
private String row='';
private Integer currentIndex=0;
public CSVColIterator(String row){
this.row = row;
}
public Boolean hasNext(){
return currentIndex < row.length();
}
public String next(){
String token='';
if(row.substring(currentIndex,currentIndex+1)==textQualifier){
Integer key=row.indexOf(textQualifier,currentIndex+1);
token = row.substring(currentIndex+1,key);
this.currentIndex = key+1;
}else{
Integer key=row.indexOf(colDelimiter,currentIndex);
if(key==-1){
key = row.length();
}
token = row.substring(currentIndex,key);
this.currentIndex=key+1;
}
return token;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment