Advertisement
mixster

CSV parser

Apr 5th, 2012
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1.     private static Iterator<String> parseCSVLine(String input) {
  2.         boolean literal = true;
  3.         boolean checkSpeech = true;
  4.         boolean inSpeech = false;
  5.         boolean checkDel = true;
  6.         char c;
  7.         String temp = "";
  8.         ArrayList<String> values = new ArrayList<String>();
  9.        
  10.         for (int i = 0; i < input.length(); i++) {
  11.             c = input.charAt(i);
  12.             if (checkDel && c == ',') {
  13.                 values.add(temp.trim());
  14.                 temp = "";
  15.                 checkSpeech = true;
  16.                 continue;
  17.             }
  18.            
  19.             if (checkSpeech && c == ' ') {
  20.                 continue;
  21.             }
  22.            
  23.             if (literal) {
  24.                 if (c == '\\') {
  25.                     literal = false;
  26.                 } else if (c == '"') {
  27.                     if (checkSpeech) {
  28.                         inSpeech = true;
  29.                         checkDel = false;
  30.                     } else if (inSpeech) {
  31.                         inSpeech = false;
  32.                         checkDel = true;
  33.                     } else {
  34.                         temp += c;
  35.                     }
  36.                 } else {
  37.                     temp += c;
  38.                 }
  39.             } else {
  40.                 temp += c;
  41.                 literal = true;
  42.             }
  43.            
  44.             checkSpeech = false;
  45.         }
  46.        
  47.         values.add(temp.trim());
  48.        
  49.         return values.iterator();
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement