-1

I read line from txt file. How i can parse this line :

shop "The best shop" "2006"

to array of strings ?

For example:

 array[0] = [shop]
 array[1] = [The best shop] 
 array[2] = [2006]

Without double quotes and spaces ?

Andy Turner
  • 122,430
  • 10
  • 138
  • 216
Pavel Klindziuk
  • 45
  • 1
  • 1
  • 7
  • 1
    Tokenize, rather than parse. – Andy Turner Feb 19 '17 at 13:34
  • Split the string on `"` to get bits inside and outside double quotes separately; then split the even-numbered elements on spaces to split the strings outside double quotes. – Andy Turner Feb 19 '17 at 13:36
  • @CKing I'm not perfectly sure, if a regex can implement escaping via `"`. Otherwise, if so, you've got your answer. – Izruo Feb 19 '17 at 13:40

1 Answers1

0
    String shop = "shop \"The best shop\" \"2006\"";

    String[] tokens = shop.split(" \"");

    for (int i = 0; i < tokens.length; i++) {
        tokens[i] = tokens[i].replace("\"", "");
        System.out.println("array[" + i + "] = [" + tokens[i] + "]");
    }

But this is for this line only :)

Pavlo Plynko
  • 568
  • 8
  • 25