-1

I need to tokenize this string.

AddItem rt456 4  12 BOOK "File Structures" "Addison-Wesley" "Michael Folk"

I need

"AddItem","rt456","12","BOOK","File Structures","Addison-Wesley","Michael Folk" 

substrings. How i can get these tokens with using split()?

Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
user3460845
  • 19
  • 1
  • 3
  • Really, `split`? Why you want to complicate your life? Wouldn't simple Pattern/Matcher be simpler? You can also write your own simple parser for that. – Pshemo Mar 25 '14 at 17:28

1 Answers1

0
List<String> list = new ArrayList<String>();
Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(str);
while (m.find())
    list.add(m.group(1));

Output:

[AddItem, rt456, 4, 12, BOOK, "File Structures", "Addison-Wesley", "Michael Folk"]
Brian
  • 2,999
  • 4
  • 25
  • 47