Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
CSV Parser
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { // simple explaination of CSV format: // https://gpdb.docs.pivotal.io/43170/admin_guide/load/topics/g-escaping-in-csv-formatted-files.html //String s = "\"Free trip to A,B\",\"5.89\",\"Special rate \"\"1.79\"\""; String s = "\"Alexandra \"\"Al,ex\"\"\",Menendez,alex.menendez@gmail.com,Miami,1 \"\"\"Alexandra Alex\"\"\""; System.out.println(s); System.out.println(parseCSV(s)); } //"Alexandra ""Alex""",Menendez,alex.menendez@gmail.com,Miami,1 """Alexandra Alex""" public static String parseCSV(String str) { int n = str.length(); char[] s = str.toCharArray(); StringBuilder sb = new StringBuilder(); List<String> tokens = new ArrayList<>(); boolean inQuote = false; for (int i = 0; i < n; i++) { if (s[i] == '"') { if (i + 1 < n && s[i + 1] == '"') { sb.append('"'); i += 1; } else inQuote = !inQuote; } else if (s[i] == ',') { if (inQuote) sb.append(','); else { tokens.add(sb.toString()); sb.setLength(0); } } else sb.append(s[i]); } if (sb.length() > 0) tokens.add(sb.toString()); return String.join("|", tokens); } }
run
|
edit
|
history
|
help
0
get top k frequent elements ( Priority Queue implementation ) Language: Editor: Layout:
Javaprog.java
Coding Challenge - 01 (Even numbers)
exp 2
java
1.5
kjhkj
Cyclically rotate array
add2
Sort arraylist bubble sort