Created
September 14, 2015 08:09
-
-
Save q3yi/3f107c3d0649694fc3cc to your computer and use it in GitHub Desktop.
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 LineReader { | |
public static List<String> splitCSV(String str, char delimiter, char escape) { | |
boolean isClose = true; | |
boolean isEscaped = false; | |
List<String> result = new ArrayList<String>(); | |
StringBuilder sb = new StringBuilder(); | |
for (char c : str.toCharArray()) { | |
if (c == delimiter) { | |
if (isClose) { | |
result.add(sb.toString()); | |
sb.setLength(0); | |
} else { | |
sb.append(c); | |
} | |
isEscaped = false; | |
} else if (c == escape) { | |
isClose = !isClose; | |
if (isEscaped) { | |
sb.append(c); | |
isEscaped = false; | |
} else { | |
isEscaped = true; | |
} | |
} else { | |
sb.append(c); | |
isEscaped = false; | |
} | |
} | |
if (sb.length() != 0) { | |
result.add(sb.toString()); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment