有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在java中使用split()标记字符串?

我需要标记这个字符串

AddItem rt456 4  12 BOOK "File Structures" "Addison-Wesley" "Michael Folk"

我需要

"AddItem","rt456","12","BOOK","File Structures","Addison-Wesley","Michael Folk" 

子字符串。如何使用split()获取这些令牌


共 (1) 个答案

  1. # 1 楼答案

    List<String> list = new ArrayList<String>();
    Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(str);
    while (m.find())
        list.add(m.group(1));
    

    输出:

    [AddItem, rt456, 4, 12, BOOK, "File Structures", "Addison-Wesley", "Michael Folk"]