从文件中读取时解析java中的字符串

时间:2017-02-19 13:34:08

标签: java string parsing quotes

我从txt文件中读取了一行。 我如何解析这一行:

shop "The best shop" "2006"

到字符串数组?

例如:

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

没有双引号和空格?

1 个答案:

答案 0 :(得分: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] + "]");
    }

但这仅适用于此行:)