Created
April 10, 2020 00:12
-
-
Save surajp/f0fc315cf3ef4a347600451420ba1a95 to your computer and use it in GitHub Desktop.
CSV Column Iterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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