splitting of string in java
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.
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?
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
You should use split() methnod from String class instead of StringTokenizer class. StringTokenizer class is less efficient than String.split() method.
If you can assure that never will there be a "," inside a pair of ", then the .split-Solution is your way to go.
精彩评论