2017-02-19 72 views
-1

我从txt文件中读取行。 我怎么能解析这条线:在从文件读取时解析java中的字符串

shop "The best shop" "2006" 

要数组的字符串?

例如:

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

没有双引号和空间?

+1

记号化,而不是解析。 –

+0

将字符串拆分为'''以分别获取双引号内外的位;然后拆分空格上的偶数元素以将字符串拆分为双引号。 –

+0

@CKing我不确定如果正则表达式可以实现通过'''逃脱。否则,如果是这样,你已经得到了你的答案。 – Izruo

回答

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] + "]"); 
    } 

但是,这是此行唯一的:)