使用带有多个分隔符的 String.split() [英] Use String.split() with multiple delimiters
本文介绍了使用带有多个分隔符的 String.split()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要根据分隔符 -
和 .
拆分字符串.下面是我想要的输出.
I need to split a string base on delimiter -
and .
. Below are my desired output.
AA.BB-CC-DD.zip
->
AA
BB
CC
DD
zip
但我下面的代码不起作用.
but my following code does not work.
private void getId(String pdfName){
String[]tokens = pdfName.split("-\.");
}
推荐答案
我认为您需要包含正则表达式 OR 运算符:
I think you need to include the regex OR operator:
String[]tokens = pdfName.split("-|\.");
你所拥有的将匹配:
[DASH 后跟 DOT] -.
不是
[DASH 或 DOT 中的任何一个] -
或 .
What you have will match:
[DASH followed by DOT together] -.
not
[DASH or DOT any of them] -
or .
这篇关于使用带有多个分隔符的 String.split()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文