I want to split this string and put the values in to my tables of database.I am thinking of using string tokenizer class or some other way. please let me the best method and how to implement it practically with code.
-
If there can be comma's between the quotes, consider a CSV parser: http://ostermiller.org/utils/CSV.html – Bart Kiers Nov 05 '10 at 08:20
-
Do it with java if you want to experiment but the correct way is to use http://dev.mysql.com/doc/refman/5.1/en/load-data.html – cherouvim Nov 05 '10 at 08:23
-
i am writing my code in java...so i have to use java only. – Nov 05 '10 at 08:29
-
1Someone correct me, but I'd say this post, http://stackoverflow.com/questions/4104456/data-parsing-from-a-file-into-java-and-then-into-a-mysql-database, http://stackoverflow.com/questions/4103884/reading-data-files and http://stackoverflow.com/questions/4103740/parsing-data-into-tables-in-java are almost duplicates. – darioo Nov 05 '10 at 09:18
4 Answers
It looks like CSV, so you could use any of the recommended Java CSV libraries: Can you recommend a Java library for reading (and possibly writing) CSV files?

- 1
- 1

- 79,991
- 11
- 123
- 154
-
+1 for library. This problem _looks_ simple until you start looking into quotes inside quotes etc. – Thorbjørn Ravn Andersen Nov 05 '10 at 08:42
Assuming there are no commas in your strings
String[] ss = s.split(",");
will do it
If you have strings such as
"Foo, Bar"
Then you will need a Pattern and regex and chop off each matched group. See http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
However your target string is unclear. Does it have a newline in or does it have two target strings? If the former you will need:
String[] ss = s.split("[,\\n]");
But I am worried that your problem is not sufficiently clearly defined

- 37,407
- 44
- 153
- 217
You should use split() methnod from String class instead of StringTokenizer class. StringTokenizer class is less efficient than String.split() method.

- 6,281
- 21
- 68
- 106
If you can assure that never will there be a "," inside a pair of ", then the .split-Solution is your way to go.

- 5,576
- 5
- 34
- 59
-
split funtion will only remove , in between the words, but I want to remove the " " also, so that I can put it in the table – Nov 05 '10 at 08:30
-
Then a CSV reading class/framework, as suggested by Peter Knego, is the way to go. – 0xCAFEBABE Nov 05 '10 at 08:42