-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
  • 209,639
  • 45
  • 409
  • 525
user3460845
  • 29
  • 1
  • 4
  • 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"]
Engineer2021
  • 3,288
  • 6
  • 29
  • 51