-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()?

1
  • 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. Commented Mar 25, 2014 at 17:28

1 Answer 1

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"]
Sign up to request clarification or add additional context in comments.

1 Comment

@user3460845: Use replaceAll("\"", "");

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.