将String.split()与多个分隔符一起使用 [英] Use String.split() with multiple delimiters

查看:162
本文介绍了将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("-|\\.");

你所拥有的将匹配 - 。不是 - a。

What you have will match "-." not a "-" or a "."

这篇关于将String.split()与多个分隔符一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆