我有一个字符串,它包含多个id及其名称:
1019-水稻分配/1022-小麦(补编)/1030-水稻BPLCVC (SLETY)/1031-水稻BPLCVC Adhoc (为K-35)
我想拆分它,并在我使用
myString.split("[/or-]");这几乎给了我想要的结果。但有一个问题,当在最后的组合中,程序名包含"-",程序也分裂了这一点,我不想要如何避免这一点。请提出一个通用的解决方案,它适用于像J-300这样的程序名称,而且不仅适用于K-35所需的输出。
发布于 2014-03-04 10:04:42
我会先在/进行拆分,得到一个ID / name -对数组,然后通过substring和第一个-的indexOf将每个字符串分开,分别获得ID和name。
发布于 2014-03-04 10:10:02
不知道这对你是否合适:
String s = "yourString";
String[] v = s.split("/|(?<=(/|^)\\d{1,999})-");
System.out.println(Arrays.toString(v));它的产出如下:
[1019, Rice Distribution, 1022, Wheat (Supplement), 1030, Rice BPLCVC (SLETY), 1031, Rice BPLCVC Adhoc (For K-35)]所以你看到最后一个元素,k-35在那里。
发布于 2014-03-04 10:10:52
String bubba = "1019-Rice Distribution/1022- Wheat (Supplement)/1030-Rice BPLCVC (SLETY)/1031-Rice BPLCVC Adhoc (For K-35)";
for(String c: bubba.split("/"){
System.out.println(c.replaceFirst("-"," ");
}https://stackoverflow.com/questions/22168592
复制相似问题