Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用许多分隔器分开一根绳子_Java - Fatal编程技术网

Java 用许多分隔器分开一根绳子

Java 用许多分隔器分开一根绳子,java,Java,我对android的编程有问题 我有这样一个字符串: Km Portagens Refeições-Almoço Refeições-Jantar Refeições-Lanche Estacionamento [Km]、[Portagens]、[Refeiçes-Almoço]、[Refeiçes]- Jantar“]、[“雷菲斯-兰切”]、[“Estacionamento”]、[“Estadia” 酒店“]、[“加索里奥”]、[“加索里纳”]、[“出租车”]、[“科博里奥”]、[“阿维昂”

我对android的编程有问题

我有这样一个字符串:

Km
Portagens
Refeições-Almoço
Refeições-Jantar
Refeições-Lanche
Estacionamento
[Km]、[Portagens]、[Refeiçes-Almoço]、[Refeiçes]- Jantar“]、[“雷菲斯-兰切”]、[“Estacionamento”]、[“Estadia” 酒店“]、[“加索里奥”]、[“加索里纳”]、[“出租车”]、[“科博里奥”]、[“阿维昂”]、[“维亚图拉”] de Aluguer“],[“Viaturas BIQ“]、[“经济体”]、[“相关方”]、[“外部方”]、[“参考方”-佩奎诺 almoço“]]

我想将其拆分,将内容保存到数组中,如下所示:

Km
Portagens
Refeições-Almoço
Refeições-Jantar
Refeições-Lanche
Estacionamento
。 . .

因此,分隔符是:

1- [
2- ]
3 - "
4 - ,
我怎样才能用这个字符分割字符串

如果我使用
[\\W]
,我会得到一些空白字符串

你能帮我吗

问候

String[] tokens = s.split("\"\\],\\[\"");
您将对第一个和最后一个令牌进行特殊处理。

  • 逃离[和],因为它们已经是正则表达式字符了
  • 包括未被\w捕获的两边的引号“”
  • 使用?对于最低匹配-最佳实践,此处并非严格必要
代码

输出

Km
Portagens
Estacionamento
Gasolina
Taxi
Economato
etc.
怎么样

[\[\]\",]{1,}
当您提到的4个字符中有一个或多个字符时,将进行拆分

String[] words = str.split("[\\[\\]\\\",]{1,}");
我建议使用


注意:这还没有经过测试。

你是否尝试过使用
\\W+
进行拆分?试试[\\W+?],也可以发布你正在使用的代码…这不好,它还会拆分
Refeições-Jantar
。我想这将丢失>1个令牌字符串(例如,“Estadia Hotel”)@KyleRenfro Yep抱歉没有发现,谢谢,更新了我的解决方案。非常感谢。这就是我需要的
StringTokenizer st =
                new StringTokenizer("[[\"Km\"],[\"Portagens\"],[\"Refeiçoes - Almoço\"],[\"Refeiçoes - Jantar\"],[\"Refeiçoes - Lanche\"],[\"Estacionamento\"],[\"Estadia Hotel\"],[\"Gasóleo\"],[\"Gasolina\"],[\"Taxi\"],[\"Combóio\"],[\"Aviao\"],[\"Viatura de Aluguer\"],[\"Viaturas BIQ\"],[\"Economato\"],[\"Correio\"],[\"Outros\"],[\"Refeiçoes - Pequeno almoço\"]]", "\",[]");


        while (st.hasMoreElements()) {
            String token = st.nextElement().toString();
            System.out.println("Token = " + token);
        }